RegEx Match issue - match not happening
Posted on 2008-10-15
I have a function that build a RegEx and applies it against some incoming data.
The incoming data:
incomingData = @"UNH+00000154600001+CONTRL:D:3:UN+CONTRL'
The RegEx created:
pattern = "^U\s*N\s*H\s*\+(?<unhCode1>.*)\+(?<unhMessageType>.*):(?<unhShortCode>.
Now when I try go:
reg = new Regex(pattern);
throw new Exception("The Incoming Data differs from the pattern ");
Now I understand why this is happening - because the match is trying to compare the pattern to the WHOLE incoming String as far as I understand, and the pattern only matches the first part of the incoming data.
What I want it to do is find the first occurance of the pattern, in the incoming data, so that if I go:
groups = reg.Match(incomingData).Groups;
Then I don't get an error, but I get the group value matched by the Regular Expression for the UNH line.