0

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!

9
  • 1
    can't reproduce regex101.com/r/nC0wV7/1 , maybe try adding a m modifer @"(?m)regex" Commented Sep 25, 2015 at 3:04
  • 2
    It returns true for both cases, although I would escape the literal . for both good practice and to make sure it's validating an actual dot (otherwise it means any character - except newline). Commented Sep 25, 2015 at 3:05
  • 1
    ideone.com/D1i1CS Commented Sep 25, 2015 at 3:39
  • 1
    If you put a backslash before the dot, then your problem was the dot, meaning any character. You don't want that. It shouldn't fail the regex, though I guess it's possible that the dot would eat digits too early and cause there to be none for the remaining groups. Commented Sep 25, 2015 at 3:45
  • 1
    do you have check any blank before or after 1.0.0.4 ,if so ,you can remove ^$ and try Commented Sep 25, 2015 at 3:46

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, bro. You saved my day. Problem is solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.