-2

I have a textbox and the only input that it should take is either a single ip address or a range in the form of: 10.2.3.4-10

I have this so far:

string txt = "10.13.11.12-100";

        string re1 = "((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])";   // IPv4 IP Address 1
        string re2 = ".*?"; // Non-greedy match on filler
        string re3 = @"\b([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\b";//"([0-255])";   // Integer Number 1

        Regex r = new Regex(re1 + re2 + re3, RegexOptions.IgnoreCase | RegexOptions.Singleline);
        Regex r2 = new Regex(re1, RegexOptions.IgnoreCase | RegexOptions.Singleline);
        Match m = r.Match(txt);
        Match m3 = r2.Match(txt);
        if (m.Success || m3.Success)
        {...}

Only thing is with that, if I have 100.100.11.11-256 it will still return true since it matches m3.Success above as true.

How to fix that?

sd_dracula
  • 3,796
  • 28
  • 87
  • 158
  • This might help you with the regex part: http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address. It might need tweaked slightly for your particular language. – JaredC Jan 09 '13 at 01:17
  • You should be able to use the answer from here http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address – sa_ddam213 Jan 09 '13 at 01:17
  • try this class http://stackoverflow.com/a/2138724/1080742 – spajce Jan 09 '13 at 01:25

1 Answers1

-1

For the IP Range Regex:

^10.2.3.(?:[4-9]|10)$

For the single IP address: Regular expression to match DNS hostname or IP Address?

Community
  • 1
  • 1
Michael
  • 3,334
  • 20
  • 27
  • I think the OP wanted that *format*, not that IP range specifically. – JaredC Jan 09 '13 at 01:23
  • That was just a sample, it should take any IP address, and any range not just from 10.2.3.4 to 10.2.3.4.10. This is for one ip address format `\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b` but not sure how to add the range to it (doesn't matter if its a valid address or not) – sd_dracula Jan 09 '13 at 01:24
  • This answer is does not correspond to the user's range. – dryleaf Oct 26 '21 at 01:52