Home > Net >  I want a regex support for characters that uses IP Address with Subnet
I want a regex support for characters that uses IP Address with Subnet

Time:11-17

I have a regex ^[a-zA-Z0-9.*?] $ that supports IP addresses like 31.202.216.280 how can I modify the given regex in a way where I could support subnets with an IP address like so 31.202.216.280/38

CodePudding user response:

Look at the pre-made regular expressions provided by these Perl modules.

Regexp::Common::net

Regexp::Common::net::CIDR

You can use them on the command line or from a Perl script. You can also just copy the regex's and use them in another language.

CodePudding user response:

With such a regex for IPs you might catch a lot of false positive. Try at least to remove a-ZA-Z. There are already great regex on SO to match IPs.

If you want to match your subnet add /[0-9]{1,3} before your string end ($). You might need to escape the slash depending on your programming language: \/.

  • Related