1

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.

4
  • 1
    Add .* after the 10? What does your string look like? How do you use the regex? Commented Jun 14, 2016 at 22:17
  • 1
    Might want to use a better regex. safaribooksonline.com/library/view/regular-expressions-cookbook/… Commented Jun 14, 2016 at 22:18
  • 1
    @Wiktor Stribizew that worked! Thank you Commented Jun 14, 2016 at 22:24
  • I posted an answer with some more tweaks, please check. Commented Jun 14, 2016 at 22:29

1 Answer 1

1

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.*".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.