Check IP adrress with regular expression
5 views (last 30 days)
Show older comments
Hi all,
I try to use regular expression to check the pattern of IP address, I tried with
IP = regexp(str, '[0-255]\.[0-255]\.[0-255]\.[0-255]', 'match')
but i can't work.
So, anyone know how to set the digit that range is from 0 to 255 to check the pattern of IP address? I tried with
IP = regexp(str, '[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?', 'match')
but it doesn't work for every situation.
Your suggestion and help is much appreciated. Thank you.
0 Comments
Answers (1)
Walter Roberson
on 18 Apr 2014
0*
to allow leading 0's.
After that you can have
1\d\d
or
2[0-4]\d
or
25[0-4]
or
\d\d
or
\d
so take all those possibilities and join them with |
0*(1\d\d|2[0-4]\d|25[0-4]|\d\d|\d)
If you call that (say) Q, then your fuller pattern becomes
((Q\.){3}Q)
Then if necessary you would put guards around it to ensure that it was not proceeded or followed by a digit or a period
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!