Python regular expressions OR

PythonRegex

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHenleyView Question on Stackoverflow
Solution 1 - PythonPaulView Answer on Stackoverflow
Solution 2 - PythonThiefMasterView Answer on Stackoverflow