PostgreSQL正则表达式边界?

mpe*_*pen 60 regex postgresql word-boundary

PostgreSQL支持\b吗?

我正在尝试,\bAB\b但它不匹配任何东西,而是(\W|^)AB(\W|$).这两个表达式基本相同,不是吗?

Dan*_*uis 77

PostgreSQL使用\m,\M,\y\Y为单词边界:

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

请参阅手册中的正则表达式约束转义.

也有[[:<:]][[:>:]],匹配一个单词的开头和结尾其中.从手册:

括号表达式有两种特殊情况:括号表达式[[:<:]][[:>:]]约束,分别在单词的开头和结尾处匹配空字符串.一个单词被定义为一个单词字符序列,既不是单词字符,也不是单词字符.单词字符是alnum字符(由ctype定义)或下划线.这是一个扩展,与POSIX 1003.2兼容但未指定,并且在用于移植到其他系统的软件中应谨慎使用.下面描述的约束转义通常是可取的(它们不再标准,但肯定更容易键入).


MD.*_*med 16

一个简单的例子

select * from table_name where column ~* '\yAB\y';
Run Code Online (Sandbox Code Playgroud)

这将匹配AB ab ab - text text ab text AB text-ab-text text AB text......

但你必须使用:

select * from sometable where name ~* '\\yAB\\y';
Run Code Online (Sandbox Code Playgroud)

如果你有standard_conforming_strings标志设置为OFF.请注意双斜线.
您可以手动设置它:

set standard_conforming_strings=on;
Run Code Online (Sandbox Code Playgroud)

然后:select * from table_name where column ~* '\yAB\y';应该工作.

  • 我使用postgres 9.3.10和`value~*'\ yAB\y'`工作得很好.你的笔记是9.2特定的吗? (3认同)

Pra*_*nde 5

文本中的精确单词搜索:

我面临以下问题。

我想搜索标题中包含“cto”作为确切词的所有联系人,但在结果中得到的结果是标题中包含“director”的结果,我使用了以下查询

select * from contacts where title ilike '%cto%';
Run Code Online (Sandbox Code Playgroud)

我还尝试在通配符周围使用空格作为“% cto %”,它与包含“ cto ”的文本匹配,得到像“vp、cto 和 manger”这样的结果,但没有准确标题为“cto”的结果。

我想要结果中的“副总裁、首席技术官和经理”和“首席技术官”,而不是结果中的“导演”

以下为我工作

select * from contacts where title ~* '\\ycto\\y';

~   Matches regular expression, case sensitive
~*  Matches regular expression, case insensitive    
Run Code Online (Sandbox Code Playgroud)