0

For example, I want to find a string contains the below string.

<a href="http://www.abc.com/Cool">Cool</a>

The "Cool" can be any string but must the same at those 2 places.

How to use Pattern and Matcher to achieve this? Thanks!

skaffman
  • 398,947
  • 96
  • 818
  • 769
shiami
  • 7,174
  • 16
  • 53
  • 68

2 Answers2

5
<a href="http://www\.abc\.com/([^"]*)">\1</a>

matches the string as specified. So, in Java:

Pattern regex = Pattern.compile("<a href=\"http://www\\.abc\\.com/([^\"]*)\">\\1</a>");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

Have you attempted a simple approach yet such as making a list of all unique words and then looping through each word, checking the occurrence count in the original string? The simple regex \b\w+\b matches words.

Here's an article explaining how to match consecutive duplicate words. You should be able to adapt this easily to your needs.

Jeff Swensen
  • 3,513
  • 28
  • 52