2010-11-10 60 views
0

例如,我想查找一個字符串包含下面的字符串。如何使用Java模式和匹配器在字符串中查找2次字符串?

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

「酷」可以是任何字符串,但必須在這兩個地方相同。

如何使用模式和匹配器來實現這一目標? 謝謝!

+0

非常相似:http://stackoverflow.com/questions/1058783/regular-expression-to-find-and-remove-duplicate-words – 2010-11-10 13:28:55

回答

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

與指定的字符串匹配。因此,在Java:

Pattern regex = Pattern.compile("<a href=\"http://www\\.abc\\.com/([^\"]*)\">\\1</a>"); 
Matcher regexMatcher = regex.matcher(subjectString); 
foundMatch = regexMatcher.find(); 
+2

非常好! (+1) – 2010-11-10 13:31:42

+0

謝謝!有用! – shiami 2010-11-10 14:18:18

0

你有沒有嘗試一個簡單的方法,但如做的所有獨特單詞的列表,然後通過每個字循環,檢查原始字符串出現計數?簡單的正則表達式\ b \ w + \ b匹配單詞。

Here's an article解釋如何匹配連續的重複單詞。你應該能夠很容易地適應你的需求。

相關問題