Reputation: 67
I am trying to use Java regular expressions to do something I could have sworn I have done many times over, but it seems I have a problem.
Basically, I am using ".*" to skip over everything I don't need until I find something I need.
It is easier for me to explain in code than in writing:
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");
Now I want my regex to find the "correct", but instead it skips over to the last match and finds "wrong".
Can anyone help me out?
Upvotes: 2
Views: 445
Reputation: 726569
You are missing the reluctant qualifier after *
- it should be .*?
instead.
Upvotes: 2