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
  • 390,936
  • 96
  • 800
  • 764
shiami
  • 7,024
  • 15
  • 51
  • 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
  • 313,408
  • 56
  • 485
  • 544
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,503
  • 27
  • 51