如何将多个.net正则表达式合并为一个?

时间:2016-03-21 18:49:23

标签: c# regex

var keyList = new[] { "AccountKey", "PrivateKey", "APIKey", "DefectiveKeyGracefulExpiration" };
var multiplePatternMatching = string.Format("({0})", string.Join("|", keyList));
var keyRegex = string.Format(@"(?s)<([\s<]?{0}[\s<]*)>.*?</\1>", multiplePatternMatching);

我有另一个正则表达式:

var passwordRegex = @"(?si)<([^\s<]*password[^\s<]*)>.*?</\1>";

如何将keyRegexpasswordRegex合并为一个正则表达式?我知道我需要使用|,但我不知道如何使用|

我正试图像这样使用var keyOrPasswordRegex = string.Format( @"(?s)<([\s<]?{0}[\s<]*)>.*?</\1>|(?si)<([^\s<]*password[^\s<]*)>.*?</\2>", multiplePatternMatching);

<job xmlns:i="..." xmlns="..."> <password>asdfasdf</password> <adminPassword>asd</adminPassword> <AccountKey>asd</AccountKey> <AccountKeyZ>asd</AccountKeyZ> ...</job>

但它不起作用

输入:

<job xmlns:i="..." xmlns="...">
 <></>
 ​<></>
​<AccountKey></AccountKey>
​<AccountKeyZ>asd</AccountKeyZ>
​...</job>

实际结果:

<job xmlns:i="..." xmlns="...">
<password></password>
<adminPassword></adminPassword>
<AccountKey></AccountKey>
<AccountKeyZ>asd</AccountKeyZ>
...</job>

预期结果:

join(;)

1 个答案:

答案 0 :(得分:2)

你需要这样的替换:

var keyList = new[] { "AccountKey", "PrivateKey", "APIKey", "DefectiveKeyGracefulExpiration" };
var multiplePatternMatching = string.Format("({0})", string.Join("|", keyList));
var rx = string.Format(@"(?si)<([^\s<]*password[^\s<]*|{0})>.*?</\1>", multiplePatternMatching);
Console.WriteLine(Regex.Replace(s, rx, "<$1></$1>"));

请参阅IDEONE demoregex demo。说明:

  • < - 文字<
  • ([^\s<]*password[^\s<]*|{0}) - (第1组)0+除空格以外的字符,<后跟单词password,后跟除空格以外的0+个字符和<AccountKeyPrivateKeyAPIKeyDefectiveKeyGracefulExpirationmultiplePatternMatching变量中列出的那些)的替代组
  • > - 文字>
  • .*?</\1> - 任意0+符号,尽可能少,直至第一个</,然后是第一个捕获组的内容>