Python regular expressions OR

Question:

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.

Asked By: Henley

||

Answers:

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

See in action here.

Answered By: Paul
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..

Answered By: ThiefMaster
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.