[英]Regex: Is it possible to use "|" for only part of match pattern?
是否可以執行以下操作:
re.match(r'someArbLongRegex{option1|option2}anotherArbLongRegex', line)
而不是必須做:
re.match(r'someArbLongRegexoption1anotherArbLongRegex|someArbLongRegexoption2anotherArbLongRegex', line)
基本上代替|
適用於整個正則表達式模式我只是希望它適用於正則表達式模式的一小部分。
嘗試使用 (?:option1|option2)
re.match(r'someArbLongRegex(?:option1|option2)anotherArbLongRegex', line)
是的。 只需使用括號。
re.match(r'someArbLongRegex((option1)|(option2))anotherArbLongRegex', line)
有時,根據數據,您知道 option1 和 option2 不會都在“行”中,並且其中至少有 1 個會。 然后你可以這樣做:
re.match(r'(someArbLongRegex)((option1)?(option2)?)anotherArbLongRegex', line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.