我的任務是創建一個正則表達式,我可以使用該正則表達式檢測多次出現的同一個單詞,並將其全部替換爲首次出現的單詞。如何讓Java在尊重單詞邊界的同時檢測反向引用?
我已經想出到目前爲止是:(\\b\\w+\\b)\\s{1}\\1
然而,這種檢測例如第二個horse
在horse horseracing
。我只想檢測完全匹配,即我希望我提供的單詞邊界得到尊重。我想,(\\b\\w+\\b)\\s{1}\\1
似乎是\\b(\\w+)\\b\\s{1}\\1
。
一些示例替換:
Goodbye bye bye world world world
- >Goodbye bye world
Sam went went to to to his business
- >Sam went to his business
- >Reya is the best player in eye game
三個以上工作正常,但是:
in inthe
變得inthe
代替in inthe
。我應該修改什麼,以便尊重單詞邊界?