PostgreSQL の正規表現による置換

2008-12-4 20:06
このエントリーをはてなブックマークに追加

PostgreSQL は以前から like 演算子の代わりに ~ 演算子で正規表現検索ができました。Version 8.1 からは、regexp_replace 関数で、正規表現による置換もできるようになっています。

  SELECT file_name, regexp_replace(file_name, '(^.*_)([0-9]+)(U_.*$)', '\\2') FROM table_name WHERE file_name ~ '(^.*_)([0-9]+)(U_.*$)';

file_name               | regexp_replace
------------------------+----------------
hoge_030914U_A01.jpg    | 030914
foo_bar_152528U_B03.jpg | 152528
test123_4317U_X_YZ.png  | 4317

こんな感じで正規表現の一部を利用した表示ができます。\1, \2, ……は、正規表現中の ( ) にマッチした内容です。バックスラッシュを表わすために、SQL 中ではさらに \ を加えて、\\1,\\2, ……のようになります。UPDATE 文でも利用できます。

  • ブックマーク :
    Warning: Use of undefined constant SBM_count_get - assumed 'SBM_count_get' (this will throw an Error in a future version of PHP) in /var/www/futuremix.org/wp-content/themes/futuremix/index.php on line 40

    Warning: Use of undefined constant show_post_count - assumed 'show_post_count' (this will throw an Error in a future version of PHP) in /var/www/futuremix.org/wp-content/themes/futuremix/index.php on line 40
    アクセス: 49,736回
  • カテゴリー : データベース
  • キーワード :

1件のコメント

  1. 置換するの?

    コメント by BlogPetのusausa2008-12-11 14:06

Sorry, the comment form is closed at this time.

70 queries. HTML convert time: 0.853 sec. Powered by WordPress. Valid XHTML
Copyright © 2003-2017 @ futuremix.org ログイン