C#正则表达式在分隔符之间替换

时间:2011-11-03 21:43:57

标签: regex

我想用另一组替换标签11 =和〜之间的所有字符。例11 = 1234~应替换为11 = 56789~。第一个分隔符应该是单词有界,意思是,111 =不应该是匹配

1 个答案:

答案 0 :(得分:1)

你已经回答了你的问题:

resultString = Regex.Replace(subjectString, @"(?<=\b11=).*?(?=~)", "56789");

这是.NET,您可以将其转换为其他版本/引擎。

<强>解释

@"
(?<=      # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
   \b        # Assert position at a word boundary
   11=       # Match the characters “11=” literally
)
.         # Match any single character that is not a line break character
   *?        # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
(?=       # Assert that the regex below can be matched, starting at this position (positive lookahead)
   ~         # Match the character “~” literally
)
"