I think I need to use an alternation construct but I can’t get it to work. How can I get this logic into one regular expression pattern?
match = Regex.Match(message2.Body, @"\r\nFrom: .+\(.+\)\r\n");
if (match.Success)
match = Regex.Match(message2.Body, @"\r\nFrom: (.+)\((.+)\)\r\n");
else
match = Regex.Match(message2.Body, @"\r\nFrom: ()(.+)\r\n");
EDIT:
Some sample cases should help with your questions
From: email
and
From: name(email)
Those are the two possible cases. I’m looking to match them so I can do
string name = match.Groups[1].Value;
string email = match.Groups[2].Value;
Suggestions for a different approach are welcome!
Thanks!
This is literally what you’re asking for:
"(?=" + regex1 + ")" + regex2 + "|" + regex3But I don’t think that’s really what you want.
With .net’s Regex, you can name groups like this:
(?<name>regex).However, your
\r\nis probably not right. That would be a literalrnFrom:. Try this instead.