有了這個例子,
whether or not apple
apple
not apple
我怎么能找到appleornot apple和 exclude whether or not apple?我的嘗試是使用負面的向后看
(?!whether or not)(([Nn]ot)?\s*apple)
但這發現所有
- 是否
not apple applenot apple
uj5u.com熱心網友回復:
由于您的消費模式從一個可選模式開始,您需要考慮這兩種情況并實作兩個單獨的后視:
(?<!\bwhether or )(?<!\bwhether or not )(?:\bnot )?\bapple\b
請參閱正則運算式演示。詳情:
(?<!\bwhether or )-whether or左側不允許立即(?<!\bwhether or not )-whether or not左側不允許立即(?:\bnot )?- 一個可選的not單詞序列和一個空格\bapple\b- 一個完整的詞apple。
另一種方法是匹配要跳過的字串并跳過它:
\bwhether\s or\s not\s apple\b(*SKIP)(*F)|(?:\bnot\s )?\bapple\b
請參閱此正則運算式演示。在這里,\bwhether\s or\s not\s apple\b匹配whether or not apple并(*SKIP)(*F)洗掉匹配,跳到失敗的位置并重新開始新的匹配搜索。
uj5u.com熱心網友回復:
您正在尋找的模式是^(?:not\s)?apple
Python
import re
re.match(r"^(?:not\s)?apple", "apple") # matches
re.match(r"^(?:not\s)?apple", "not apple") # matches
re.match(r"^(?:not\s)?apple", "whether or not apple") # does not match
JavaScript
/^(?:not\s)?apple/.test("apple"); // matches
/^(?:not\s)?apple/.test("not apple"); // matches
/^(?:not\s)?apple/.test("whether or not apple"); // does not match
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427082.html
標籤:正则表达式
上一篇:具有至少一個字母和至少一個數字的特定長度的字母數字字串的正則運算式
下一篇:如何忽略RegExp中的標簽
