(字邊界)我都存儲在一個字符串文本文件名爲Str
看起來像這樣的內容:如何找到一組詞的使用正則表達式
Str = "Cat is an animal\n" +
"Cat is small\n" +
"Cat is a pet\n";
我寫這個代碼搜索的字Cat
:
Pattern pattern = Pattern.compile("(\\bCat\\b)");
Matcher match = pattern.matcher(Str);
while (match.find()) {
String tempStr = "I found " + match.group() + "\n";
}
上面產生這樣的輸出:
I found Cat
I found Cat
I found Cat
這是我的問題。如何使用關鍵字Cat查找整個句子,以便輸出結果爲:
I found Cat is an animal
I found Cat is small
I found Cat is a pet
這是什麼正則表達式?
你有什麼試過?你沒有顯示任何找到該句子的企圖/線索。我也會說這個問題缺乏細節/背景。 – 2013-02-28 20:19:21
是的,我找到了解決我的問題的方案。我嘗試了簡單的正則表達式Vlad L建議並返回符合我預期的輸出。謝謝! – 2013-02-28 20:58:39