[英]Regex to find and replace any word that is not after another word
I'm currently running a simple find-and-replace, on strings like this: 我正在这样的字符串上运行一个简单的查找和替换:
1. User.Name "John"
2. User.Age 20
3. Name.Length 5
However, trying to replace Name
with WHATEVER
results in this: 但是,尝试使用WHATEVER
替换Name
导致:
1. User.WHATEVER "John"
2. User.Age 20
3. WHATEVER.Length 5
I needed to change line 3, but not line 1. How do I check if the current word is after a dot ( .
) and skip replacing that word? 我需要更改第3行,但不能更改第1行。如何检查当前单词是否在点( .
)之后并跳过替换该单词?
I'm in .NET 4.0 and my regex currently looks like this: 我在.NET 4.0中,我的正则表达式目前看起来像这样:
result = new Regex(@"\b" + oldWord + @"\b").Replace(text, newWord);
You can use a negative lookbehind on .
你可以使用负面的lookbehind .
: (?<!\\.)
: (?<!\\.)
That gives: 这给了:
result = new Regex(@"\b(?<!\.)" + oldWord + @"\b").Replace(text, newWord);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.