Reputation: 1251
I have two strings: "word1|word2"
, "word2|word1"
Is there any way with regexp to extract word2
from these strings if it contains word1
?
Upvotes: 0
Views: 133
Reputation: 114661
Or use a look around...
(?<=^word1\|).*|.*(?=\|word1$)
That way you can use
match.Value
And disregard Groups and Captures altogether.
Upvotes: 0
Reputation: 1000
I believe something like this would do it:
var input = new String[] { "word1|word2", "word2|word1" };
var regexp = @"word1\|(?<GROUP>.*)|(?<GROUP>.*)\|word1";
foreach (var word in input)
{
var match = Regex.Match(word, regexp, RegexOptions.IgnoreCase);
Console.WriteLine(match.Groups["GROUP"].Value);
}
But your requirements are quite unclear to me, so please feel free to ellaborate :-)
Upvotes: 1
Reputation: 6372
I would go for a more flexible approach like splitting on the "|" and checking each part, and saving the other one if you find a match.
If you really need to use regex, something like (word1\|(.*)|(.*)\|word1)
would put word2 into backreference 1.
You may need to tweak the (.*)
part, depending on whether this string is by itself or embedded in other text that should not be matched.
Upvotes: 0
Reputation: 35572
split
the string by |
into array and then find
any string you like to find
Upvotes: 1