分别匹配每个事件的Java正则表达式
我有一个正则表达式:分别匹配每个事件的Java正则表达式,java,regex,pattern-matching,Java,Regex,Pattern Matching,我有一个正则表达式: <a href(.*foo.bar.*)a> 改为: <a href(.*?foo\.bar.*?)a> 它消除了贪婪。实际点应转义到\。更改为: <a href(.*?foo\.bar.*?)a> 它消除了贪婪。真正的点应该转义到\。希望下面的代码能帮助您: int noOfTimefoundString = 0; Pattern pattern = Pattern.compile("<a href=\"https:
<a href(.*foo.bar.*)a>
改为:
<a href(.*?foo\.bar.*?)a>
它消除了贪婪。实际点应转义到\。
更改为:
<a href(.*?foo\.bar.*?)a>
它消除了贪婪。真正的点应该转义到
\。
希望下面的代码能帮助您:
int noOfTimefoundString = 0;
Pattern pattern = Pattern.compile("<a href=\"https://foo.bar");
Matcher matcher = pattern.matcher(body);
List<String> matches = new ArrayList<String>();
while (matcher.find()) {
matches.add(matcher.group());
noOfTimefoundString++;
}
Iterator matchesItr = matches.iterator();
while(matchesItr.hasNext()){
System.out.println(matchesItr.next());
}
System.out.println("No. of times search string found = "+noOfTimefoundString);
int noOfTimefoundString=0;
Pattern=Pattern.compile(“希望下面的代码能帮助您:
int noOfTimefoundString = 0;
Pattern pattern = Pattern.compile("<a href=\"https://foo.bar");
Matcher matcher = pattern.matcher(body);
List<String> matches = new ArrayList<String>();
while (matcher.find()) {
matches.add(matcher.group());
noOfTimefoundString++;
}
Iterator matchesItr = matches.iterator();
while(matchesItr.hasNext()){
System.out.println(matchesItr.next());
}
System.out.println("No. of times search string found = "+noOfTimefoundString);
int noOfTimefoundString=0;
Pattern Pattern=Pattern.compile(“使用*?
而不是*
)。贪婪的量词匹配尽可能多的字符,而不情愿的量词匹配单个查找操作中最少的字符
此外,如果要匹配“foo.bar”的文本,请使用foo\.bar
。使用*?
而不是*
。贪婪的量词匹配尽可能多的字符,而不情愿的量词匹配单个查找操作中最少的字符
此外,如果您想匹配“foo.bar”的文本,请使用foo\.bar
.请给我们看一下搜索匹配的代码好吗?@JREN:添加了搜索代码如果你在使用html,你应该使用html解析器……请给我们看一下搜索匹配的代码好吗?@JREN:添加了搜索代码如果你在使用html,你应该使用html解析器……是的,但我需要将它们存储到某个地方e、 我需要带标签的完整URL。是的,但我需要将它们存储到某个地方。我需要带标签的完整URL。谢谢,就像解释一样。谢谢,就像解释一样。问题发生了,开始看起来也很贪婪。正则表达式将匹配整个部分,但应该只取结尾。问题发生了,开始看起来很贪婪此外,正则表达式将匹配整个部分,但只应取结尾。