Reputation: 1469
I had used Regex string:
versionPattern = @"^\d+(.\d+){3}$"
to check format of string with 4 number have hyphen is .
xxx.xxx.xxx.xxx or x.x.x.x ...
But it return true with string:
1.0.0.123
and return false with string:
1.0.0.4
My code for this:
if (Regex.IsMatch(svnShopLoorTable.Rows[i].ItemArray[2].ToString(), versionPattern))
{
//MessageBox.Show("OK");
}
else
{
//MessageBox.Show("Should be: x.x.x.x");
s += "\r\nProgram " + svnShopLoorTable.Rows[i].ItemArray[1].ToString() + " of SHOPFLOOR has wrong version format: "
+ svnShopLoorTable.Rows[i].ItemArray[2].ToString() + " should be formated as: " + "x.x.x.x";
Console.WriteLine(s);
}
When svnShopLoorTable.Rows[i].ItemArray[1].ToString()
is 1.0.0.123
it's ok, not display s
. But when svnShopLoorTable.Rows[i].ItemArray[1].ToString()
is 1.0.0.4
, it's display log in console:
Program SetupSheet of SHOPFLOOR has wrong version format: 1.0.0.4 should be formated as: x.x.x.x
I don't know why this problem occur. Please help me to explain and solve this. Appreciate any help!
Upvotes: 0
Views: 392
Reputation: 326
I'm going to take a wild-guess and assume that you have some white-space before or after invalidating your regex.
Your regular expression is valid when that line contains that and only that pattern.
Make sure you do not have any white-space preceding or following "1.0.0.4", or modify your regex to be less strict:
versionPattern = @"\d+(.\d+){3}"
Or to handle whitespace:
versionPattern = @"^\s*\d+(.\d+){3}\s*$"
Also you should put a backslash ("\") before the dot ("."), as the dot matches any character in regex.
Meaning this:
^\d+(.\d+){3}$
Would also match this:
1a2b3c4
But this would not:
^\d+(\.\d+){3}$
It would only work if the intervening characters were actual dots.
Edit: Now that I see your code I realize that you can also probably just get a away with a Trim, assuming I'm not wrong.
svnShopLoorTable.Rows[i].ItemArray[1].ToString().Trim()
Upvotes: 1