C# 使用正则表达式替换字符,但不替换转义字符
我有一个字符串,我想使用C# 使用正则表达式替换字符,但不替换转义字符,c#,regex,replace,escaping,C#,Regex,Replace,Escaping,我有一个字符串,我想使用Regex将[替换为{,但是如果[被转义,请忽略它,如下所示: abc[def\[ghi 到 我该怎么做呢?非常感谢!使用此: Regex regexObj = new Regex( @"(?<= # Make sure that this is true before the start of the match: (?<!\\) # There is no single backslash, (?:\\\\)* #
Regex
将[
替换为{
,但是如果[
被转义,请忽略它,如下所示:
abc[def\[ghi
到
我该怎么做呢?非常感谢!使用此:
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);
Regex regexObj=新的Regex(
@(?要替换的[you’s want replace]之前是否总是有空格?如果是的话,就用“{”替换“[”。这真的是实际的规则吗?规则不应该是“用大括号替换未替换的括号”?另外,像[foo]bar{baz\[bam}
”这样的字符串呢?你真的希望结果是{foo]bar{baz\[bam}
?这个怎么办?[abc[efg[hij
。应该是:{abc{efg[hij
还是只替换第一个,所以它变成了{abc[efg[hij
?@MohamedTarek:我想全部替换,TimPietzcker给了我正确的解决方案!谢谢
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);