I have a string ABCD:10,20,,40;1/1;1/2,1/3,1/4 I want to split the string into the following parts:
ABCD — splited by :
10,20,,40 — splited by ;
1/1 — splited by ;
1/2,1/3,1/4 — splited by ;
Why the following regular expression does not work for me ?
string txt = @"ABCD:10,20,,40;1/1;1/2,1/3,1/4";
Regex reg = new Regex(@"\b(?<test>\w+):(?<com>\w+);(?<p1>\w+);(?<p2>\w+)");
Match match = reg.Match(txt);
The
,and/character will not be matched by\w.\wmatches letters, numbers, and underscores only.It’s better to use
[^;]+to get everything but;‘s for what you are trying to do:I left the
testcapture group alone, assuming it would always be[a-zA-Z0-9_]+.