如何将 python 正则表达式转换为 java 正则表达式?
How to convert python regex to java regex?
我在 Python 中有一些正则表达式,我需要将其转换为 java。我知道我想要正则表达式做什么,但我只是不知道如何转换它。
这是 python 中的表达式:^172\.(1[6789]|2\d|30|31)\.
。我希望它捕获任何类型的 IP 地址,例如 172.X
,其中 X 的范围从 16 到 31。
这适用于 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)
它恰当地捕捉到了我的期望:
['19']
['24']
[]
['19']
但我试图将它们转换为 java 但它不起作用。似乎我应该能够转换为 java 正则表达式,只需向每个 \
添加一个 \
即可正确转义?喜欢 ^172\.(1[6789]|2\d|30|31)\.
但还是不符合我的要求。在这种情况下,python 和 JAVA 正则表达式之间的区别我错过了什么?
我没有容易获得的 java
代码,但我尝试了这个工具: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/ 因为它将任何字符串放入 "find",即使它是 "blah" ].
那么,如何验证我的 java 正则表达式是否正确?
Java8没有findall()
的等价物,所以需要自己写find()
loop and gather the results into a List
,像这样:
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);
}
输出
[19]
[24]
[]
[19]
或者当然,由于您的正则表达式可以找到最多一个匹配项,您的代码实际上应该是:
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();
}
输出
19
24
19
我在 Python 中有一些正则表达式,我需要将其转换为 java。我知道我想要正则表达式做什么,但我只是不知道如何转换它。
这是 python 中的表达式:^172\.(1[6789]|2\d|30|31)\.
。我希望它捕获任何类型的 IP 地址,例如 172.X
,其中 X 的范围从 16 到 31。
这适用于 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)
它恰当地捕捉到了我的期望:
['19']
['24']
[]
['19']
但我试图将它们转换为 java 但它不起作用。似乎我应该能够转换为 java 正则表达式,只需向每个 \
添加一个 \
即可正确转义?喜欢 ^172\.(1[6789]|2\d|30|31)\.
但还是不符合我的要求。在这种情况下,python 和 JAVA 正则表达式之间的区别我错过了什么?
我没有容易获得的 java
代码,但我尝试了这个工具: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/ 因为它将任何字符串放入 "find",即使它是 "blah" ].
那么,如何验证我的 java 正则表达式是否正确?
Java8没有findall()
的等价物,所以需要自己写find()
loop and gather the results into a List
,像这样:
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);
}
输出
[19]
[24]
[]
[19]
或者当然,由于您的正则表达式可以找到最多一个匹配项,您的代码实际上应该是:
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();
}
输出
19
24
19