Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
 Regex regex = new Regex(@"a|b");
Match match = regex.Match(line);
 if (match.Success)
{
}


I want to know if the line matched on "a" or "b" .. because based on that result i want to do different things

What I have tried:

my best solution was to split the regex itself

 if (match1.Success)
{
}
if (match2.Success)
{
}
Posted
Updated 12-Mar-17 12:25pm

The other alternative is to use groups: Regular Expression Reference: Capturing Groups and Backreferences[^] I'd use Named groups to make it more obvious:
(?<GroupA>a)|(?<GroupB>b)
That way you can inspect the Regex.Groups property by name and determine which matched.
 
Share this answer
 
Comments
Bryian Tan 12-Mar-17 18:30pm    
Nice!!!. I had the same idea and was playing with this concept.
Here is an example using GetGroupNames method to capture the group name. At the bottom you can write an if statement, f matchedGroupName == "abc" then ...

reg_multiple C# - rextester[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900