Reputation: 164
I am parsing an IP, and I don't care about anything, but the IP. Here is what I have, but I don't care what follows after the '10', and just want to know if the String matches the IP:
[0-9]{1,3}\\.[0-9]{1,3}\\.(16|249)\\.10
What can I add into this to make it ignore everything else? This IP will be at the very beginning of the String every time as well.
Upvotes: 1
Views: 868
Reputation: 626728
If your string starts with a specific IP pattern, and you are using String#matches()
, just append a word boundary after 10
and use .*
after it:
"(?s)\\d{1,3}\\.\\d{1,3}\\.(?:16|249)\\.10\\b.*"
The (?s)
is added to make sure you will match the whole string that can contain newlines.
Instead of a \b
you may use (?!\d)
("(?!\\d)"
) to disallow matching IPs ending with 100
rather than 10
.
NOTE that the first and second parts (\\d{1,3}
) can be enhanced by replacing them with (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
and the regex would look like "(?s)(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:16|249)\\.10\\b.*"
.
Upvotes: 1