0

I have some regular expressions in Python which I need to convert to java. I know what I want the regular expression to do, but I just don't know how to convert it.

Here is the expression in python: ^172\.(1[6789]|2\d|30|31)\.. I want it to capture any sort of ip address like 172.X where X ranges from 16 to 31.

This works in python:

import re
pattern='^172\\.(1[6789]|2\\d|30|31)\\.'
test_strings = ['172.19.0.0', '172.24.0.0', '172.45.0.0', '172.19.98.94']
for string in test_strings:
    print re.findall(pattern, string)

and it appropriately captures what I expect:

['19']
['24']
[]
['19']

But I tried to convert these to java and itdoesn't work. It seems like I should be able to convert to a java regex simply by adding a \ to each \ to escape correctly? like ^172\\.(1[6789]|2\\d|30|31)\\.

but it is still not matching the way I want. what am I missing about the differences between python and JAVA regular expression in this case?

I do not have the java code easily available, but I tried this tool: http://java-regex-tester.appspot.com/, and I set the Target Text to 172.19.0.0 and it doesn't match, but it does "Find". However, when I input "blah" as the Target Text it ALSO puts something in the "Find" section...so I'm not sure I trust this tool http://java-regex-tester.appspot.com/ because it puts any string in "find", even when it is "blah".

So, how do I verify that my java regular expression is correct?

6
  • 2
    "but it is still not matching the way I want" can we see how exactly you are using this regex in Java? What is your input, expected and actual result? Commented Apr 17, 2017 at 16:01
  • Can we see how it is being used in python too? Commented Apr 17, 2017 at 16:06
  • Guess: You're using matches(). Don't, because that always matches against the entire input. Use find(). Commented Apr 17, 2017 at 16:11
  • @Andreas you mean in java? Commented Apr 17, 2017 at 16:14
  • Yes, I mean in Java. That is what you're asking, right? How to do it in Java? And my guess is that you're using the wrong Java method. Commented Apr 17, 2017 at 16:14

1 Answer 1

2

Java 8 does not have the equivalent of findall(), so you need to write your own find() loop and gather the results into a List, like this:

Pattern pattern = Pattern.compile("^172\\.(1[6789]|2\\d|30|31)\\.");
String[] test_strings = {"172.19.0.0", "172.24.0.0", "172.45.0.0", "172.19.98.94"};
for (String string : test_strings) {
    List<String> list = new ArrayList<>();
    for (Matcher matcher = pattern.matcher(string); matcher.find(); )
        list.add(matcher.group(1));
    System.out.println(list);
}

Output

[19]
[24]
[]
[19]

Or course, since your regex can find at most one match, your code should really be:

Pattern pattern = Pattern.compile("^172\\.(1[6789]|2\\d|30|31)\\.");
String[] test_strings = {"172.19.0.0", "172.24.0.0", "172.45.0.0", "172.19.98.94"};
for (String string : test_strings) {
    Matcher matcher = pattern.matcher(string);
    if (matcher.find())
        System.out.println(matcher.group(1));
    else
        System.out.println();
}

Output

19
24

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

2 Comments

I'm not of age to vote, but +1 @Andreas! Is this true for scala as well?
@jaja You should still be able to accept the answer by clicking the checkmark. Sorry, I don't know Scala.

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.