假設你必須找到一個文檔中的雙字,這是如何做到這一點:
\b(\w+)\s+\1\b
這裏是解剖:
<!--
\b(\w+)\s+\1\b
Options:^and $ match at line breaks
Assert position at a word boundary «\b»
Match the regular expression below and capture its match into backreference number 1 «(\w+)»
Match a single character that is a 「word character」 (letters, digits, and underscores) «\w+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) «\s+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the same text as most recently matched by capturing group number 1 «\1»
Assert position at a word boundary «\b»
-->
調用的組號是唯一的呼叫/包括前一組中的圖案的方式。希望這個halp。請參閱here以供參考。
我需要使用組來獲取匹配列表。所以,我希望它可以包含在模式匹配中,而不需要對現有的正則表達式進行任何修改。 – chaitu 2012-04-19 03:59:40