下面的文本是使用正則表達式從巨大文本中過濾出來的,我必須找到沒有porc和pig的狗和貓的句子。如何選擇2個單詞之間有3個或更多字符的單詞
What, a Dog, a Rat, a Mouse, a Cat to scratch a man to
Where Iuliet liues, and euery Cat and Dog,
Which first (perchance) shee'l proue on Cats and Dogs,
glass, and from setting up memorials of departed cats and dogs.
Thinking,' etc., 1873, p. 82.), "A dog frames a general concept of cats or
dog, who never passed a cat who lay sick in a basket, and was a great
要了解什麼是上面我用正則表達式:
^(?!.\*porc.\*)(?!.\*pig.\*)(?=.\*\bdog\b.\*)(?=.\*\bcat\b.\*).\*
現在,我必須找到狗和貓之間的話具有3個字符等等。
我想:
^(?!.\*porc.\*)(?!.\*pig.\*)(?=.\*\bdog\b.\*)(?=.\*\bcat\b.\*)dog(?:\s?\w{3,})+cat
它不工作。
任何人都有如何解決它的想法?
在給出的示例文本中,您想要檢索什麼?我對此不太清楚。如果您可以顯示一些示例輸出... –
根據我的經驗,當涉及複雜的邏輯時(如在其他單詞之間搜索單詞),我可以說正則表達式不是一個好工具。我建議使用不同的工具來提取所需的子字符串,然後將簡單的搜索正則表達式應用於每個子字符串。 – Anton
您可以使用'indexOf'來檢查而不是在正則表達式中查找。如果字符串有多個「狗」和「貓」,你的預期輸出是什麼? – nhahtdh