{{1}你不需要把{1}的结果写进一个{1},这样你就不需要把^ 1的结果写进^ 1中: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 list = new ArrayList<>();
for (Matcher matcher = pattern.matcher(string); matcher.find(); )
list.add(matcher.group(1));
System.out.println(list);
}
输出
^{pr2}$
当然,由于regex最多只能找到一个匹配项,因此您的代码应该是: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正则表达式转换为java正则表达式?的全部内容,更多相关python内容请搜索靠谱客的其他文章。
发表评论 取消回复