I am trying to replace street with st only if street isn't followed by any alphabet. Replacement is allowed if after street EITHER there is non-alphabet character OR end of string.
我正试着用st来代替街道,除非街上没有任何字母。如果街道后面有非字母表字符或字符串尾,则允许替换。
I am trying to achieve this in Postgresql 9.5 regex_replace function. Sample query i wrote:
我试图在Postgresql 9.5 regex_replace函数中实现这一点。我写查询样例:
select regexp_replace('super streetcom','street(?!=[a-z])','st');
的选择regexp_replace(“超级streetcom”、“街(? ! =[a - z])”,“圣”);
street here shouldn't have been replaced by st since street is followed by 'c'. So the expected output is 'super streetcom' but the output i am getting is 'super stcom'.
这里的街道不应该被st取代,因为street后面跟着c。所以预期输出是" super streetcom "但我得到的输出是" super stcom "
Any help for why i am getting the unexpected output and what can be the right way to achieve the intended result.
对于为什么我得到了意想不到的输出,以及什么是实现预期结果的正确方法,有什么帮助吗?
2 个解决方案
#1
1
A lookahead construct looks like (?!...)
, all what follows ?!
is a lookahead pattern that the engine will try to match, and once found, the match will be failed.
一个前瞻结构看起来像(?!),接下来会发生什么?!是引擎将尝试匹配的前瞻模式,一旦找到,匹配将失败。
It seems you need to match a whole word street
. Use \y
, a word boundary:
看来你需要把整条街搭配起来。使用\y,一个词的边界:
select regexp_replace('super streetcom street','\ystreet\y','st');
See the online demo
查看在线演示
From the docs:
从文档:
\y
matches only at the beginning or end of a word只在单词的开头或结尾匹配
#2
1
This looks like a syntax issue. Try: ?!
instead of ?!=
. e.g.
这看起来像是一个语法问题。试题:? !而不是? !=。如。
select regexp_replace('super street','street(?![a-z])','st');
will return
将返回
super st
#1
1
A lookahead construct looks like (?!...)
, all what follows ?!
is a lookahead pattern that the engine will try to match, and once found, the match will be failed.
一个前瞻结构看起来像(?!),接下来会发生什么?!是引擎将尝试匹配的前瞻模式,一旦找到,匹配将失败。
It seems you need to match a whole word street
. Use \y
, a word boundary:
看来你需要把整条街搭配起来。使用\y,一个词的边界:
select regexp_replace('super streetcom street','\ystreet\y','st');
See the online demo
查看在线演示
From the docs:
从文档:
\y
matches only at the beginning or end of a word只在单词的开头或结尾匹配
#2
1
This looks like a syntax issue. Try: ?!
instead of ?!=
. e.g.
这看起来像是一个语法问题。试题:? !而不是? !=。如。
select regexp_replace('super street','street(?![a-z])','st');
will return
将返回
super st