Jaanus
Jaanus

Reputation: 16551

Java regex matching each occurence separately

I have this regex:

<a href(.*foo.bar.*)a>

For this string, it gives me only 1 match, but I need it to give 3 matches.

<a href="https://foo.bar/1">First</a> RANDOM TEXT COULD BE HERE <a href="https://foo.bar/2">Second</a> RANDOM TEXT COULD BE HERE <a href="https://foo.bar/3">Third</a>

So each a href should be individual.

How could I accomplish this?

EDIT:

This code searches for matches:

Pattern pattern = Pattern.compile("<a href(.*foo.bar.*)a>");
Matcher matcher = pattern.matcher(body);
List<String> matches = new ArrayList<String>();
while (matcher.find()) {
    matches.add(matcher.group());
}

Upvotes: 1

Views: 151

Answers (3)

D-Nesh
D-Nesh

Reputation: 1

Hope below code will help you:

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);

Upvotes: 0

dexjq23
dexjq23

Reputation: 386

Use .*? instead of .*. The greedy quantifier matches characters as many as possible, while the reluctant quantifier matches the least number of characters in a single find operation.

Besides, use foo\.bar if you intend to match a literal text of "foo.bar".

Upvotes: 1

dda
dda

Reputation: 6213

Change to:

<a href(.*?foo\.bar.*?)a>

It removes the greediness. And real dots should be escaped to \..

Upvotes: 6

Related Questions