Regexp在文本PostgreSQL中只找到6位数字

Cri*_*isS 5 regex postgresql

我需要建立在PostgreSQL的查询和我需要查找包含6位数字(例如,所有的文本输入000999,019290,998981,234567,等).问题是在字符串的开头或结尾处不需要该数字.

我尝试过但没有工作:

  • [0-9]{6} - 返回超过6位数的部分
  • (?:(?<!\d)\d{6}(?!\d)) - postgresql不知道lookbehind
  • [^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

h2o*_*ooo 6

如果PostgreSQL支持单词边界,请使用\b:

\b(\d{6})\b
Run Code Online (Sandbox Code Playgroud)

编辑:

\b在PostgreSQL意味着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
Run Code Online (Sandbox Code Playgroud)

应该管用.

\m(\d{6})\M
Run Code Online (Sandbox Code Playgroud)

应该工作.

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 $)
Run Code Online (Sandbox Code Playgroud)

新编辑:

根据您的编辑,您应该能够这样做:

(^|[^\d])(\d+)([^\d]|$)
Run Code Online (Sandbox Code Playgroud)