5

PostgreSQL でクエリを作成する必要があり、6 桁の数字 (例: 000999019290、など)998981を含むすべてのテキスト エントリを検索する必要があります。234567問題は、文字列の先頭または末尾に数字が必要ないことです。

私は試してみましたが、うまくいきませんでした:

  • [0-9]{6}- 6 桁を超える数値の一部を返します
  • (?:(?<!\d)\d{6}(?!\d))- postgresql は後読みについて認識していません
  • [^0-9][0-9]{6}[^0-9] とそのバリエーションがありますが、役に立ちません。

必要なスキルがないため、独自の Perl/C 関数を構築することは実際には選択肢ではありません。どの正規表現を使用できるか、または現時点で私を逃れている他のトリックはありますか?

編集

入力サンプル:

  • aa 0011527 /CASA-> NOTHING を返す必要があります
  • aa 001152/CASA-> 戻る必要があります001152
  • aa001152/CASA-> 戻る必要があります001152
  • aa0011527/CASA-> NOTHING を返す必要があります
  • aa001152 /CASA-> 戻る必要があります001152
4

2 に答える 2

6

PostgreSQL が単語境界をサポートしている場合は、次を使用します\b

\b(\d{6})\b

編集

\bPostgreSQL では を意味するbackspaceため、単語の境界ではありません。

http://www.postgresql.org/docs/8.3/interactive/functions-matching.html#FUNCTIONS-POSIX-REGEXP\yただし、単語の境界として使用できることを説明しますmatches only at the beginning or end of a word

\y(\d{6})\y

動作するはずです。

\m(\d{6})\M

も動作するはずです。

PostgreSQL 正規表現で一致する単語の完全なリスト:

Escape  Description
\A      matches only at the beginning of the string (see Section 9.7.3.5 for how this differs from ^)
\m      matches only at the beginning of a word
\M      matches only at the end of a word
\y      matches only at the beginning or end of a word
\Y      matches only at a point that is not the beginning or end of a word
\Z      matches only at the end of the string (see Section 9.7.3.5 for how this differs from $)

新しい編集:

あなたの編集に基づいて、これを行うことができるはずです:

(^|[^\d])(\d+)([^\d]|$)
于 2013-01-24T14:11:26.877 に答える
0

@h2ooooooo が提案したものを使用して、次のクエリを作成できました。

SELECT cleantwo."ID",cleantwo."Name",cleantwo."Code"
FROM
(
SELECT cleanone."ID",cleanone."Name",unnest(cleanone."Code") as "Code" -- 3. unnest all the entries received using regexp_matches (get all combinations)
FROM 
(
SELECT sd."ID", sd."Name", regexp_matches(sd."Name", '(^|[^\d])(\d+)([^\d]|$)')
    as "Code"
FROM "T_SOME_DATA" sd
WHERE substring(sd."Name" from 1 for 15) ~('(^|[^\d])(\d+)([^\d]|$)') -- 1. get all data possible
) as cleanone
WHERE cleanone."Code" IS NOT NULL -- 2. get data where code IS NOT NULL (remove useless entries)
) as cleantwo
WHERE length(cleantwo."Code")=6 -- 4. get only the combinations relevant to my initial requirement (codes with length 6)<br/>

これを見つけるのにかなりの時間がかかったので、同じ状況の誰かに役立つことを願っています. 幸運を!

于 2013-01-24T15:10:46.420 に答える