a + (ab or cd ) + g
is my expression. How can I write a regex in Python to match these?
views:
84answers:
1
A:
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)
jellybean
2010-09-09 07:01:44
Just to make sure it's clear, the `+` has a particular meaning in regular expressions. So if you want to literally match the `+` character itself, you need to escape it: `"(a\\+b)|(cd\\+e)"`
MatrixFrog
2010-09-09 07:07:50
Of course. I was assuming the OP had that in mind.
jellybean
2010-09-09 07:08:53
@MatrixFrog: Ok ... after his last edit I understand he was looking for literal "+" indeed.
jellybean
2010-09-09 07:25:34
Can't this be re-written as `re.search(r"a\W*\+\W*(ab|cd)\W*\+\W*g", your_string)`? Use a raw string so you don't ahve to escape everything twice, and you don't need the extra pair of `()` around the `|`.
Mark
2010-09-09 07:49:13
@Mark: Good point, thanks.
jellybean
2010-09-09 08:13:31