2
我想用Java正则表达式来做一些我可以发誓的事情,我已经做了很多次,但似乎我有一个问题。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");
现在,我想我的正则表达式来找到“正确的”,而是将其跳过的最后一场比赛,并认为“错误” 。
任何人都可以帮我吗?
'。*'是贪婪的,它会尝试消耗尽可能多的字符。我不确定,但尝试使用'。*?',这是不愿意的。 – nhahtdh
。*?会做你想要什么,但[1] [1] [解析正则表达式的HTML被认为是有害的。]:http://stackoverflow.com/questions/1732348/regex-match-open-tags -except-XHTML-自足标签/ 1732454#1732454 –