50
votes

Suppose I want a regular expression that matches both "Sent from my iPhone" and "Sent from my iPod". How do I write such an expression?

I tried things like:

re.compile("Sent from my [iPhone]|[iPod]") 

but doesn't seem to work.

2
Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info. - ridgerunner
You can use pythex.org to quickly and easily experiment with your Python regular expressions. I find this to be very helpful whenever I'm struggling to find the correct syntax. - J Jones

2 Answers

82
votes
re.compile("Sent from my (iPhone|iPod)")
27
votes
re.compile("Sent from my (?:iPhone|iPod)")

If you need to capture matches, remove the ?:.

Fyi, your regex didn't work because you are testing for one character out of i,P,h,o,n,e or one character out of i,P,o,d..