java regex fetch ip
How can i fetch 192.168.1.101
usin regex in java in following string ,however Bcast
may be or not present
' inet a开发者_C百科ddr:192.168.1.101 Bcast:192.168.1.255 Mask:255.255.255.0'
with leading space
Use something like this:
(?<=inet addr:)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
I really feel Abhishek Simon's answer for the regex is an overkill. You are just extracting, not validating if it is a legal IP address!
For Bcast use something like below, obviously:
(?<=Bcast:)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
To get all IPs, use without the initial lookahead.
You might be better of doing some string operations like splitting on and
:
to get the IPs. I leave it to you to decide.
use (([1]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9])[.]){3}(([2]([0-4][0-9]|[5][0-5])|[0-1]?[0-9]?[0-9]))
Here see this snapshot, it also fetches the bcast ip
You can use: ([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3})
The first tagged expression will be the first ip address that appears in the expression.
I often use Regular expression test sites to help troubleshoot regular expressions.
精彩评论