How to build a regex for an IP Addres Range?

Jesus Nava
Tera Guru

Hello experts please I need your help, how can a regex to accept an IP range be built? for example:

34.3.4.4 - 34.3.4.5

I have this regex from 

https://community.servicenow.com/community?id=community_question&sys_id=0cf3a62ddbb8a010fa192183ca96198e

^(?=.*?[1-9])[0-9()-]+$

but it is not working as I need it, I need that the user can add and IP address with the character - so as to add a range, how can that be done?

Thank you

1 ACCEPTED SOLUTION

Muhammad Khan
Mega Sage
Mega Sage

Hi Jesus,

 

Try this in variable validation regex

^[0-9]+.[0-9]+.[0-9]+.[0-9]+ \- [0-9]+.[0-9]+.[0-9]+.[0-9]+$

View solution in original post

16 REPLIES 16

@Chris Helming, Yeah you are right, it will not work for IP Address validation but it might help @Jesus Nava  to add/validate dash " - " between IP Ranges.

As for IP Address validation, there is already an OOTB Variable Validation Regex Available for IP Addresses, So he might/could have used that one or the one provided by @Muhammad Yousaf.

@Jesus Nava, reconsider your selection for better results and help future users having the same requirement.

By the way I have created a regex for IP Addresses ( separated by " - " ) Validation, can you please validate that one as well, whether it is working or not ?

^(([0]|[1-9][0-9]{0,1}|[1][0-9][0-9]|[2][0-5][0-5])\.){3}([0]|[1-9][0-9]{0,1}|[1][0-9][0-9]|[2][0-5][0-5]) \- (([0]|[1-9][0-9]{0,1}|[1][0-9][0-9]|[2][0-5][0-5])\.){3}([0]|[1-9][0-9]{0,1}|[1][0-9][0-9]|[2][0-5][0-5])$

_ChrisHelming
Tera Guru

You should not use that regex. It would accept input of "9999999 - 123456789" which is very clearly not an IP address range.