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

And I have another regex:

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

How to combine keyRegex and passwordRegex into one regex? I known that I need to use | but I don't know how.

I'm trying to use | like this:

var keyOrPasswordRegex = string.Format( @"(?s)<([\s<]?{0}[\s<]*)>.*?</\1>|(?si)<([^\s<]*password[^\s<]*)>.*?</\2>", multiplePatternMatching);

but it's doesnt work

Input:

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

Actual result:

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

Expected result:

<job xmlns:i="..." xmlns="...">
<password></password>
<adminPassword></adminPassword>
<AccountKey></AccountKey>
<AccountKeyZ>asd</AccountKeyZ>
...</job>
11
  • 2
    Already answered here: stackoverflow.com/questions/869809/combine-regexp Commented Mar 21, 2016 at 18:51
  • I think you have a problem with a backreference here, you need to be careful not to break the order of capturing groups. Use var multiplePatternMatching = string.Format("(?:{0})", string.Join("|", keyList)); var keyRegex = string.Format(@"(?s)<([^\s<]*{0}[^\s<]*)>.*?</\1>", multiplePatternMatching); Commented Mar 21, 2016 at 19:08
  • @WiktorStribiżew thanks, and how to combine keyRegex with passwordRegex ? Commented Mar 21, 2016 at 19:11
  • I think by adding "password" to the keyList. Commented Mar 21, 2016 at 19:13
  • 1
    Ok, I see that key regex means a regex for tags that include key word and their content must be reset. Try Regex.Replace(input, @"(?si)<([^\s<]*password[^\s<]*|(?:AccountKey|PrivateKey|APIKey|DefectiveKeyGracefulExpiration))>.*?</\1>", "<$1></$1>"). See this demo. Commented Mar 21, 2016 at 22:00

1 Answer 1

2

You need an alternation like this:

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

See the IDEONE demo and a regex demo. Explanation:

  • < - a literal <
  • ([^\s<]*password[^\s<]*|{0}) - (Group 1) 0+ characters other than whitespace and < followed with a word password and followed with 0+ characters other than whitespace and < or an alternation group of AccountKey, PrivateKey, APIKey or DefectiveKeyGracefulExpiration (those listed in the multiplePatternMatching variable)
  • > - a literal >
  • .*?</\1> - any 0+ symbols, as few as possible, up to the first </ followed with the contents of the first capture group and >.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.