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

Sure No problem


***Mark Correct or Helpful if it helps.***

This is the best answer. To have a range just duplicate it and add a dash.

^(?:(?: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]?)\s?-\s?(?:(?: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]?)$

 

An even better method would be to have two variables: one for IP range start and one for IP range end.

Thanks Yes I duplicated it and it was working.


***Mark Correct or Helpful if it helps.***

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]+$

What is the type of your variable, is it IP Address or Single Line Text ?