Reputation: 1916
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>
Upvotes: 0
Views: 647
Reputation: 626738
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 >
.Upvotes: 2