问题
I am validating a Ipv4 address by a regex and it does not support subnet mask.
^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])$
Can some one help me with the regex which supports mask as well.
Here is a working example of this regex: demo
回答1:
Add (?:/[0-2]\d|/3[0-2])? at the end of your regex. You can also simplify the regex:
^([01]?\d\d?|2[0-4]\d|25[0-5])(?:\.(?:[01]?\d\d?|2[0-4]\d|25[0-5])){3}(?:/[0-2]\d|/3[0-2])?$
回答2:
In your example, if you want it to match both adresses, remove beginning ^ and trailing $
来源:https://stackoverflow.com/questions/30590193/regex-to-match-ipv4-with-mask