匹配不带正则表达式的字符串 [英] Match string not preceded by another with a regular expression
本文介绍了匹配不带正则表达式的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
声明
Regex.Replace("XB", "([^A])B", "$1AB")
按预期产生 XAB
.有人可以解释一下为什么
produces XAB
, as expected. Can someone explain me why
Regex.Replace("XBB", "([^A])B", "$1AB")
不是会产生 XABAB
,但是会产生 XABB
吗?就像正则表达式解析器到达第二个B时不再知道前一个字符.
does not produce XABAB
, but XABB
? It's like the regex parser no longer has knownledge of the preceding character when it reaches the second B.
最终,我想用 AB
替换所有未以 A
开头的 B
.
Ultimately, I want to replace all B
s not preceded by a A
by AB
.
推荐答案
所有B都不以AB开头的A开头.
查找:(?<!A)B
替换: AB
这篇关于匹配不带正则表达式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文