First time poster, long time user. I CAN NOT figure out why this data with this regular expression is passing when it should fail. The quick overview is that I have a text file with pipe delimited data in it. I’m reading each line in one at a time and comparing against a regex for pass/fail.
Here is the data in question:
|A|00032004|00032004|25 S Kings Highway||Cape Giradeau|MO|63701|345800886888|0000254575|091091|RGT Foods, Inc.|1|345800886888|1|345800886888|1|601103061404806|1|003241699917|0|000000000000|0|000000000000|0|000000000000|0|000000000000|
|A|00032005|00032005|1009 Kings Hwy||Rolla |MO|65401|345800885880|0000254564||RGT Foods, Inc.|1|345800885880|1|345800885880|1|601103061404798|1|003241699925|0|000000000000|0|000000000000|0|000000000000|0|000000000000|
Here’s the basic breakdown:
|D,U,or A|ID#|ID#|St Add1|St Add2|City|ST|Zip|#|#|Name|bool|#|bool|#|bool|#|bool|#|bool|#|bool|#|bool|#|bool|#|
This is my regular expression (warning: it’s kinda long):
^[\|]{1}[DUA]{1}[\|]{1}[0-9,A-Z]{8}[\|]{1}[0-9,A-Z]{8}[\|]{1}.{0,25}[\|]{1}.{0,25}[\|]{1}.{0,25}[\|]{1}[A-Z,a-z]{2}[\|]{1}[0-9]{5}[\|]{1}[A-Z,a-z,0-9]{12}[\|]{1}[A-Z,a-z,0-9]{10}[\|]{1}.{0,25}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}[0,1]{1}[\|]{1}[0-9]{15}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}[0,1]{1}[\|]{1}[0-9]{12}[\|]{1}
This is my regex function:
//Compare the entire line at once
public static bool MatchCCRegEx(string spLine)
{
try
{
Regex CCLineCheck = new Regex(
Properties.Settings.Default.CCRegExValidationString);
Match CCLineMatch = CCLineCheck.Match(spLine);
if (CCLineMatch.Success)
return true;
else
return false;
}
catch (Exception RegExCheckExc)
{
WELogger.LogEvent("3",
"Error running RegEx check on this line:\r\n"
+ spLine + "\r\n" + RegExCheckExc.ToString());
Environment.Exit(9);
return false;
}
}
The example data I gave should fail because there is an extra field between # and Name with a value of 091091. The second line should also fail because of the extra field (but it’s empty on that one). I’ve stared at the regex for hours, because it looks like to me at the “#|#|Name|bool”, 091091 would’ve been put in for name and pass, but “RGT Foods, Inc.” shouldn’t pass as a 0 or 1…but both lines pass regex, what am i doing wrong?
Thanks.
It’s a match because
.{0,25}not only matches theRGT Foods, Inc.but also the091091|before it.If you know your “free” fields won’t contain any pipes, replace
.{0,25}with[^|]{0,25}. (“Zero to 25 not-a-pipe characters”.)Also, for readability, note
[\|]can be written[|]or\|.{1}can be removed completely; the default is “match once”.[A-Z,a-z,0-9]matches A-Z, a-z, 0-9 and commas. You probably mean[A-Za-z0-9]. Similarly[0,1]should be[01]and[0-9,A-Z]should be[0-9A-Z].Honestly, though, if you know your free fields can’t contain a pipe, I’d just
String.Spliton the pipe and validate each field separately. That regex is a nightmare.