I have two strings: "word1|word2"
, "word2|word1"
Is there any way with regexp to extract word2
from these strings if it contains word1
?
split
the string by |
into array and then find
any string you like to find
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 :-)
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.
Or use a look around...
(?<=^word1\|).*|.*(?=\|word1$)
That way you can use
match.Value
And disregard Groups and Captures altogether.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.