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>
Anatoly
  • 1,908
  • 4
  • 25
  • 47
  • 2
    Already answered here: http://stackoverflow.com/questions/869809/combine-regexp – Pedro G. Dias Mar 21 '16 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);` – Wiktor Stribiżew Mar 21 '16 at 19:08
  • @WiktorStribiżew thanks, and how to combine `keyRegex` with `passwordRegex` ? – Anatoly Mar 21 '16 at 19:11
  • I think by adding "password" to the `keyList`. – Wiktor Stribiżew Mar 21 '16 at 19:13
  • @WiktorStribiżew No, because `password` it's completely different regex – Anatoly Mar 21 '16 at 19:20
  • Without examples, it is not possible to help you more. `<([\s<]?{0}[\s<]*)>.*?\1>` makes no sense. – Wiktor Stribiżew Mar 21 '16 at 19:31
  • I am putting my children to bed, I will have a look in half an hour. – Wiktor Stribiżew Mar 21 '16 at 19:46
  • Thanks, the problem is that my `passwordRegex` is with `i` and `^`, `keyRegex` without. And `passwordRegex` have `*password` and `keyRegex` have `?{0}` – Anatoly Mar 21 '16 at 19:48
  • 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](http://ideone.com/ks55aM).** – Wiktor Stribiżew Mar 21 '16 at 22:00
  • @WiktorStribiżew Thank you very much! You save my live! It works! Set your answer not in the comment if you want – Anatoly Mar 22 '16 at 08:50
  • I posted the answer below. – Wiktor Stribiżew Mar 22 '16 at 09:11

1 Answers1

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 >.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563