Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式替换字符,但不替换转义字符_C#_Regex_Replace_Escaping - Fatal编程技术网

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);