1

I have a regex thats working normally (when i tried through online regex checking websitesites). This should not allow 1234.1234.1234.1234 but while I am using it in asp.net,it is allowing even those values.

Any suggestion?

var ipfilter = new RegExp("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?$)");

2 Answers 2

1

.NET regex differs from JavaScript one immensely. However, in this case, it is a regular problem: the dot must be preceded with a literal backslash, or placed inside a character class. I suggest the latter as it is less error-prone, and you need to add a ^ (start of string) anchor:

var rx = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)[.](25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?$)";
1
  • 1
    Thanks ..You are genius.
    – Vicky
    Oct 23, 2015 at 13:54
1

Is the online regex checking website you used testing regex for .NET? .NET regex differs slightly from Javascript regex.

https://regex101.com/ - you can test against .NET on this by selecting .NET from the flavor options on the left.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.