2015-09-11 54 views
-1

我有一个字符串是这样的:C#正则表达式替换为自定义的JSON路径

Books[?(@.Author=='auth1 or auth2' or @.Author=='auth3 or auth4' or @.Author=='auth5 or auth6')].Revision 

我需要的输出如下使用Regex.Replace:

Books[?(@.Author=='auth1 or auth2' || @.Author=='auth3 or auth4' || @.Author=='auth5 or auth6')].Revision 

我尝试使用Regex.Replace(string, @"(?<filter>@\..*) or @", "${filter} || @")。但输出结果如下:

Books[?(@.Author=='auth1 or auth2' or @.Author=='auth3 or auth4' || 
@.Author=='auth5 or auth6')].Revision 

回答

1

我应该使用肯定的前瞻断言。

Regex.Replace(string, @" or(?=\[email protected]\.)", " ||"); 

Regex.Replace(string, @" or(?=\[email protected]\.Author==')", " ||"); 

(?=...)称为其声称的比赛必须跟

\s+一个或多个空格

@. @和点正向前查找。

+0

现在工作。非常感谢! “?=”是什么意思? – quldude