Write a regex for a pattern?
a + (ab or cd ) + g
is my expression. How can I write a regex 开发者_如何学Cin Python to match these?
To search this regex in a string, say
re.search("a\\+((ab)|(cd))\\+g", your_string)
To extract matches, use re.findall
etc. No need to copy&paste the docs here. :)
EDIT: Updated after OP changed the regex.
If you want it to match whitespace in between, things get pretty ugly ...
re.search("a\W*\\+\W*((ab)|(cd))\W*\\+\W*g", your_string)
精彩评论