2

我有一个字符串,我想用它Regex来替换[to {,但如果[被转义,请忽略它,如下所示:

abc[def\[ghi

abc{def\[ghi

我怎样才能做到这一点?非常感谢!

4

1 回答 1

3

用这个:

Regex regexObj = new Regex(
    @"(?<=     # Make sure that this is true before the start of the match:
     (?<!\\)   # There is no single backslash,
     (?:\\\\)* # but if there are backslashes, they need to be an even number.
    )          # End of lookbehind assertion.
    \[         # Only then match a bracket", 
    RegexOptions.IgnorePatternWhitespace);
于 2013-04-11T07:21:32.790 回答