开发者

How to find a 2-times string in a string using Java Pattern and Matcher?

开发者 https://www.devze.com 2023-01-24 14:50 出处:网络
For example, I want to find a string contains the below string. <a href=\"http://www.abc.com/Cool\">Cool</a>

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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消