-1

I have a page where a user can add an IP address to a whitelist, whose format is verified if it is a valid IP.

I'd like to add functionality so that regex's can also be input. I would like to verify that the regex matches a valid IP address (ie. the regex entered by the user is a subset of the regex that is specified in the code).

IP_Regex: ^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

Example: A user must input a string matching the specifications of IP_Regex (such as 10.111.111.111) or a subset of it (such as 12(?>\.\d{1,3}){3})

I'm not sure how to go about this. Most posts seem to just cite math theory but don't mention how to go about this when programming.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Check [this post](http://stackoverflow.com/questions/5865817/regex-to-match-an-ip-address). – Wiktor Stribiżew Dec 31 '15 at 01:24
  • It's not clear how your question relates to the title. You don't mention subsets anywhere in the body of your question. Can you edit your question to be more clear? – Jordan Running Dec 31 '15 at 01:32
  • If I were a user, I'd expect to have simpler means of specifying an IP address starting with `12.` than being forced to write `12(?>\.\d{1,3}){3}`. – Armali Feb 16 '18 at 07:57

1 Answers1

0

I don't think it is dangerous to allow your users to input regexes, so you don't have to be 100% accurate.

Therefore I would randomly generate some slightly invalid ips and make sure the regexes fail on those.

Mitch VanDuyn
  • 2,838
  • 1
  • 22
  • 29