Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression

Status
Not open for further replies.

jrwinterburn

IS-IT--Management
Jul 26, 2004
72
GB
Hi All,

I wonder if someone could help me with a regular expression query. I have a form in ASP.NET which currently has a field for IP address. I currently use a regualr expression to ensure that the user can only enter one IP address, and the regexp goes as such:

[0-9]{1,3}(.[0-9]{1,3}){3,3}

Now this works fine. However, I want to change this field so that the user can enter in either one or two IP addresses, separate by a comma, as such:

123.123.123.123

or:

123.123.123.123,456.456.456.456

I've tried a few variations but can't quite get it right. Any ideas would be appreciated.

Thanks

Jon
 
([0-9]{1,3}.){3}[0-9]{1,3}(,([0-9]{1,3}.){3}[0-9]{1,3})?

I think that would do. Although you should adapt for IP6 if necessary.

Sean. [peace]
 
The following regex will ensure that they only enter valid values (ie. 0 - 255) for the ip address too. It also checks for the optional second ip address as per your request which is seperated by a comma:

(?:(?: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]?)(,(?:(?: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]?))?

Sean. [peace]
 
Thanks very much, that makes sense. No, IPv6 is not a concern at the moment.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top