Java正则表达式跳过匹配

时间:2012-05-29 01:24:14

标签: java regex

我正在尝试使用Java正则表达式做一些我可以发誓的事情,我做了很多次,但似乎我遇到了问题。

基本上,我使用“。*”跳过我不需要的所有东西,直到找到我需要的东西。

我在代码中解释比写作更容易:

String str = "class=\"c\" div=34234542234234</span>correct<?> blah=12354234234 </span>wrong<";      
Pattern regex = Pattern.compile("class=\"c\".*</span>([^<]*)");     
Matcher matcher = regex.matcher(str);       
boolean found = false;
while (matcher.find()) {
    found = true;
    System.out.println ("Found match: " + matcher.group(1));            
}       
if (!found)
    System.out.println("No matches found");

现在我希望我的正则表达式找到“正确”,但它会跳到最后一个匹配并发现“错误”。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

*之后您错过了reluctant qualifier - 它应该是.*?

相关问题