Check IP adrress with regular expression
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Walter Roberson
2014-4-18
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!