eocron
eocron

Reputation: 63

What match is occured in Regex

Let's imagine i have some regex pattern:

(a)|(b)|(c)

How to identify what match has been triggered? Is there some kind of index of match?

I can check all groups for null or compare global Value field with group Value fields or separate pattern to many and check them through line of "if", but its kinda crappy and gives additional complexity. Is regex don't have some finite state value?

Upvotes: 1

Views: 48

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

Sure you can do this:

match.Groups[1].Success // true or false
match.Groups[2].Success // true or false
match.Groups[3].Success // true or false

You could also name your groups to make it easier to follow:

(?<foo>a)|(?<bar>b)|(?<baz>c)
match.Groups["foo"].Success // true or false
match.Groups["bar"].Success // true or false
match.Groups["baz"].Success // true or false

Upvotes: 3

Related Questions