I tried to write an expression to test the following pattern:
digit [0-9] 1 time for sure
"Point"
digit [0-9] 1-2 times
"Point"
digit [0-9] 1-3 times
"Point"
number [0-9] 1-3 times or βhyphenβ
For example, these are legal numbers:
1.10.23.5 1.10.23.-
is not:
10.10.23.5 1.254.25.3
I used RegexBuddy to write the following pattern:
[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-
Everything seems perfect in RegexBuddy, but in my code I believe in illegal numbers (e.g. 10.1.1.1)
I wrote the following method to test this pattern:
public static bool IsVaildEc(string ec) { try { if (String.IsNullOrEmpty(ec)) return false; string pattern = @"[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3}|[0-9]\.[0-9]{1,2}\.[0-9]{1,3}\.-"; Regex check = new Regex(pattern); return check.IsMatch(ec); } catch (Exception ex) {
What am I doing wrong?
source share