PostgreSQL 正则表达式字边界?
PostgreSQL 支持 \b
吗?
我正在尝试 \bAB\b
但它不匹配任何内容,而 (\W|^)AB(\W|$)
则匹配。这两个表达本质上是相同的,不是吗?
Does PostgreSQL support \b
?
I'm trying \bAB\b
but it doesn't match anything, whereas (\W|^)AB(\W|$)
does. These 2 expressions are essentially the same, aren't they?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PostgreSQL 使用
\m
、\M
、\y
和\Y
作为字边界:请参阅 正则表达式约束转义。
还有
[[:<:]]
和[[:>:]]
,它们匹配单词的开头和结尾。来自手册:PostgreSQL uses
\m
,\M
,\y
and\Y
as word boundaries:See Regular Expression Constraint Escapes in the manual.
There is also
[[:<:]]
and[[:>:]]
, which match the beginning and end of a word. From the manual:一个简单的例子
这将匹配
AB
ab
ab - 文本
文本ab
文本AB
文本-ab-文本
text AB text
...但是你必须使用:
如果你有
standard_conforming_strings
标志设置为
关闭
。请注意双斜杠。您可以手动设置它:
然后:
select * from table_name where column ~* '\yAB\y';
应该可以工作。A simple example
This will match
AB
ab
ab - text
text ab
text AB
text-ab-text
text AB text
...But you have to use:
in case you have
standard_conforming_strings
flagset to
OFF
. Note the double slashes.You can set it manually :
Then :
select * from table_name where column ~* '\yAB\y';
should work.文本中的精确单词搜索:
我面临以下问题。
我想搜索标题中包含“cto”作为确切单词的所有联系人,但在结果中得到标题中包含“director”的结果,我使用以下查询
我还尝试使用通配符周围的空白作为“% cto %” ,它与包含“cto”的文本进行匹配,得到了“vp、cto 和 manger”等结果,但没有得到确切标题为“cto”的结果。
我希望结果中出现“副总裁、首席技术官和经理”和“首席技术官”,但不希望结果中出现“总监”
以下内容对我有用
Exact word search in text:
I was facing following problem.
I wanted to search all contacts which has 'cto' as exact word in titles, but in results was getting results with title having 'director' in it, I was using following query
I also tried with whitspaces around wildcard as '% cto %', it was getting matched with text which contains ' cto ', got results like 'vp, cto and manger', but not results with exact title as 'cto'.
I wanted both 'vp, cto and manger' and 'cto' in results, but not 'director' in results
Following worked for me