如果模式结束时如何禁用匹配

How to disable matching if pattern when it comes at the end

如果模式出现在行尾,我想禁用匹配模式。 例如我的字符串是 "Hello, world" 我也想匹配这个字符串和 "Hello, world!" 字符串。 到目前为止,我尝试了

SELECT 'Hello, world' SIMILAR TO '%([\-\,\.\s\?\!]?)world([\-\,\.\s\?\!]+)%', 
       'Hello, world!' SIMILAR TO '%([\-\,\.\s\?\!]?)world([\-\,\.\s\?\!]+)%' 

但第一个匹配 returns 错误。

更新
需要匹配的词:
你好,世界!
再见了,世界。
嗨,世界
这个世界真奇妙。

不匹配的词:
你好世界
你好,世界。
这个问题被认为是世界性问题。

怎么样:

SELECT 'Hello, world' ~* '\yworld\y';

~* 匹配正则表达式且不区分大小写

\y 匹配单词边界

根据 standard_conforming_strings 参数的值,您可能必须使用 2 个反斜杠:

SELECT 'Hello, world' ~* '\yworld\y';