tags:

views:

84

answers:

1

a + (ab or cd ) + g is my expression. How can I write a regex in Python to match these?

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
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
Of course. I was assuming the OP had that in mind.
jellybean
@MatrixFrog: Ok ... after his last edit I understand he was looking for literal "+" indeed.
jellybean
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
@Mark: Good point, thanks.
jellybean