I'm using C# Regex class. I'm trying to split two strings from one. The source (input) string is constructed in following way:
first part must match PO|P|S|[1-5] (in regex syntax).
second part can be VP|GZ|GAR|PP|NAD|TER|NT|OT|LO (again, regex syntax). Second part can occur zero or one time.
Acceptable examples are "PO" (one group), "POGAR" (both groups PO+GAR), "POT" (P+OT)...
So I've use the following regex expression:
Regex r = new Regex("^(?<first>PO|P|S|[1-5])(?<second>VP|GZ|GAR|PP|NAD|TER|NT|OT|LO)?$");
Match match = r.Match(potentialToken);
When potentialToken is "PO", it returns 3 groups! How come? I am expecting just one group (first).
match.Groups are {"PO","PO",""}
Named groups are OK - match.Groups["first"] returns 1 instance, while match.Groups["second"].Success is false.
<
in front ofsecond
. Also, have you checked theSuccess
values of the groups when retrievingmatch.Groups
by index? – O. R. Mapper