我是靠谱客的博主 长情招牌,这篇文章主要介绍python java正则表达式_如何将python正则表达式转换为java正则表达式?,现在分享给大家,希望可以做个参考。

{{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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部