
Contents
使うテーブル
前回の最後(実行コマンドまとめ)が終わった時点。↓ がテーブル定義で、列# \d kenpg_seesaa_list Table "public.kenpg_seesaa_list" +-----------+------+-----------+ | Column | Type | Modifiers | +-----------+------+-----------+ | ymd | date | | | url | text | | | title | text | | | tags | text | | | html_file | text | | | html | text | |<- contents +-----------+------+-----------+
substring 関数で、マッチなしの場合
今は知らないけど、自分が書いていた頃の# select ymd, substring(html, '[^<]+') from kenpg_seesaa_list; +------------+-----------+ | ymd | substring | +------------+-----------+ | 2015-05-23 | | | 2015-04-19 | | | 2015-04-16 | | | 2015-04-15 | | | 2015-04-14 | | ...
マッチしなかった行は
なおドキュメントの表に載ってる構文は
HTML
regexp_matches 関数で、マッチなしの場合
形式は# select ymd, regexp_matches(html, '[^<]+', 'i') from kenpg_seesaa_list; +-----+----------------+ | ymd | regexp_matches | +-----+----------------+ +-----+----------------+ (0 rows)
今回のようにマッチ有無だけ知りたければ、これで終わり。substring
もし、いくつかの行にマッチした場合は ↓ こうなります。結果が常に配列で返り、部分マッチ(括弧内)があれば配列の各要素になる仕様です。
# select ymd, regexp_matches(html, 'regexp_matches .{10}', 'i') from kenpg_seesaa_list; +------------+-----------------------------------------+ | ymd | regexp_matches | +------------+-----------------------------------------+ | 2015-01-16 | {"regexp_matches の結果が配列でなく複"} | | 2014-07-03 | {"regexp_matches でファイル名、サイズ"} | | 2014-07-02 | {"regexp_matches を使い、属性名と値の"} | | 2014-01-06 | {"regexp_matches を使って次のように書"} | | 2013-10-30 | {"regexp_matches で一度にマッチさせる"} | +------------+-----------------------------------------+ (5 rows)
regexp_matches 関数で「非マッチ行も結果に含める」場合
このように# select ymd, reg from kenpg_seesaa_list left join regexp_matches(html, 'regexp_matches .{10}') as r (reg) on true order by reg nulls last; +------------+-----------------------------------------+ | ymd | reg | +------------+-----------------------------------------+ | 2014-07-03 | {"regexp_matches でファイル名、サイズ"} | | 2013-10-30 | {"regexp_matches で一度にマッチさせる"} | | 2015-01-16 | {"regexp_matches の結果が配列でなく複"} | | 2014-07-02 | {"regexp_matches を使い、属性名と値の"} | | 2014-01-06 | {"regexp_matches を使って次のように書"} | | 2015-04-13 | | | 2015-04-12 | | | 2015-04-11 | | | 2015-04-10 | | | 2015-04-09 | | ...

LEFT JOIN ... ON TRUE
↓ 別の方法。JOIN
# select ymd, ( select regexp_matches(html, 'regexp_matches .{10}') ) as reg from kenpg_seesaa_list order by reg nulls last; +------------+-----------------------------------------+ | ymd | reg | +------------+-----------------------------------------+ | 2014-07-03 | {"regexp_matches でファイル名、サイズ"} | | 2013-10-30 | {"regexp_matches で一度にマッチさせる"} | | 2015-01-16 | {"regexp_matches の結果が配列でなく複"} | | 2014-07-02 | {"regexp_matches を使い、属性名と値の"} | | 2014-01-06 | {"regexp_matches を使って次のように書"} | | 2015-04-13 | | | 2015-04-12 | | | 2015-04-11 | | | 2015-04-10 | | | 2015-04-09 | | ...

substring と regexp_matches の基本的な違い
今回の関心は「POSIX• 機能
regexp_matches
# select ymd, substring(html, 100, 200) from kenpg_seesaa_list; +------------+-------------------------------------------------------------------------------------------------+ | ymd | substring | +------------+-------------------------------------------------------------------------------------------------+ | 2015-05-23 | " /> +| | | +| | | http://kenpg.bitbucket.org/ に移転しました : 研究に使うポスグレ +| | |