PostgreSQL 正则表达式字边界?

发布于 2024-09-25 03:46:08 字数 135 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘过的浮云 2024-10-02 03:46:08

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 

请参阅 正则表达式约束转义

还有 [[:<:]][[:>:]],它们匹配单词的开头和结尾。来自手册

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

PostgreSQL uses \m, \M, \y and \Y as word boundaries:

\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 

See Regular Expression Constraint Escapes in the manual.

There is also [[:<:]] and [[:>:]], which match the beginning and end of a word. From the manual:

There are two special cases of bracket expressions: the bracket expressions [[:<:]] and [[:>:]] are constraints, matching empty strings at the beginning and end of a word respectively. A word is defined as a sequence of word characters that is neither preceded nor followed by word characters. A word character is an alnum character (as defined by ctype) or an underscore. This is an extension, compatible with but not specified by POSIX 1003.2, and should be used with caution in software intended to be portable to other systems. The constraint escapes described below are usually preferable (they are no more standard, but are certainly easier to type).

无名指的心愿 2024-10-02 03:46:08

一个简单的例子

select * from table_name where column ~* '\yAB\y';

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

但是你必须使用:

select * from sometable where name ~* '\\yAB\\y';

如果你有 standard_conforming_strings 标志
设置为关闭。请注意双斜杠
您可以手动设置它:

set standard_conforming_strings=on;

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

A simple example

select * from table_name where column ~* '\yAB\y';

This will match AB
ab
ab - text
text ab
text AB
text-ab-text
text AB text ...

But you have to use:

select * from sometable where name ~* '\\yAB\\y';

in case you have standard_conforming_strings flag
set to OFF. Note the double slashes.
You can set it manually :

set standard_conforming_strings=on;

Then :select * from table_name where column ~* '\yAB\y'; should work.

讽刺将军 2024-10-02 03:46:08

文本中的精确单词搜索:

我面临以下问题。

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

select * from contacts where title ilike '%cto%';

我还尝试使用通配符周围的空白作为“% cto %” ,它与包含“cto”的文本进行匹配,得到了“vp、cto 和 manger”等结果,但没有得到确切标题为“cto”的结果。

我希望结果中出现“副总裁、首席技术官和经理”和“首席技术官”,但不希望结果中出现“总监”

以下内容对我有用

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

~   Matches regular expression, case sensitive
~*  Matches regular expression, case insensitive    

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

select * from contacts where title ilike '%cto%';

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

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

~   Matches regular expression, case sensitive
~*  Matches regular expression, case insensitive    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文