gpt4 book ai didi

java - 使用java从数据之间提取子字符串的正则表达式

转载 作者:行者123 更新时间:2023-12-01 14:19:52 26 4
gpt4 key购买 nike

String line = "asdasdasdasd <meta name=\"generator\" content=\"WordPress 3.5.2\" /> asdasdasdasdasd";
Pattern p = Pattern.compile("<meta name=\"generator\" content=\"WordPress\\s+([\\d.]+)\" />");
Matcher m = p.matcher(line);
if(m.matches())
System.out.println(m.group(1));
else
System.out.println("not found");

我使用的正则表达式没有给出期望的结果。我想要从提供的字符串中获取 WordPress 版本。

最佳答案

Matcher#matches()匹配字符串的开头。因此,您需要为完整的字符串构建正则表达式。

或者,您可以使用Matcher#find()仅使用字符串相关部分的正则表达式:

Pattern p = Pattern.compile("content=\"WordPress\\s+([\\d.]+)\"");
Matcher m = p.matcher(line);
if(m.find())
System.out.println(m.group(1));
else
System.out.println("not found");

关于java - 使用java从数据之间提取子字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17704936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com