Is it possible to do something like the following:
re.match(r'someArbLongRegex{option1|option2}anotherArbLongRegex', line)
as opposed to having to do:
re.match(r'someArbLongRegexoption1anotherArbLongRegex|someArbLongRegexoption2anotherArbLongRegex', line)
Basically instead of |
applying to the whole regex pattern I just want it to apply to one small part of the regex pattern.
尝试使用 (?:option1|option2)
re.match(r'someArbLongRegex(?:option1|option2)anotherArbLongRegex', line)
Yes. Just use parenthesis.
re.match(r'someArbLongRegex((option1)|(option2))anotherArbLongRegex', line)
Sometimes, depending on the data, you know that option1 and option2 won't both be in 'line', and at least 1 of them will. Then you can just do this:
re.match(r'(someArbLongRegex)((option1)?(option2)?)anotherArbLongRegex', line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.