Python regular expressions OR









up vote
28
down vote

favorite
5












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.










share|improve this question



















  • 1




    Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info.
    – ridgerunner
    Dec 22 '11 at 22:10















up vote
28
down vote

favorite
5












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.










share|improve this question



















  • 1




    Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info.
    – ridgerunner
    Dec 22 '11 at 22:10













up vote
28
down vote

favorite
5









up vote
28
down vote

favorite
5






5





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.










share|improve this 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.







python regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 22 '11 at 20:55









Cédric Julien

53.1k1296109




53.1k1296109










asked Dec 22 '11 at 20:53









Henley Chiu

6,4182086159




6,4182086159







  • 1




    Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info.
    – ridgerunner
    Dec 22 '11 at 22:10













  • 1




    Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info.
    – ridgerunner
    Dec 22 '11 at 22:10








1




1




Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info.
– ridgerunner
Dec 22 '11 at 22:10





Looks like you would greatly benefit by brushing up on the basics. See: Python Regular Expression HOWTO and/or regular-expressions.info.
– ridgerunner
Dec 22 '11 at 22:10













2 Answers
2






active

oldest

votes

















up vote
52
down vote



accepted










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





share|improve this answer
















  • 24




    Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
    – Chris Tonkinson
    Dec 22 '11 at 20:59










  • How is this working? The output is: Out[25]: ['iPhone']
    – lapinkoira
    Oct 19 '16 at 13:47






  • 6




    @lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
    – Paulpro
    Oct 19 '16 at 14:55







  • 4




    @lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
    – Paulpro
    Oct 19 '16 at 16:14







  • 1




    The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
    – Paulpro
    Oct 19 '16 at 16:20

















up vote
21
down vote













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






share|improve this answer
















  • 3




    No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
    – PaulMcG
    Dec 22 '11 at 23:29










  • This should be the accepted answer
    – lapinkoira
    Oct 19 '16 at 14:17










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8609597%2fpython-regular-expressions-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
52
down vote



accepted










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





share|improve this answer
















  • 24




    Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
    – Chris Tonkinson
    Dec 22 '11 at 20:59










  • How is this working? The output is: Out[25]: ['iPhone']
    – lapinkoira
    Oct 19 '16 at 13:47






  • 6




    @lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
    – Paulpro
    Oct 19 '16 at 14:55







  • 4




    @lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
    – Paulpro
    Oct 19 '16 at 16:14







  • 1




    The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
    – Paulpro
    Oct 19 '16 at 16:20














up vote
52
down vote



accepted










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





share|improve this answer
















  • 24




    Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
    – Chris Tonkinson
    Dec 22 '11 at 20:59










  • How is this working? The output is: Out[25]: ['iPhone']
    – lapinkoira
    Oct 19 '16 at 13:47






  • 6




    @lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
    – Paulpro
    Oct 19 '16 at 14:55







  • 4




    @lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
    – Paulpro
    Oct 19 '16 at 16:14







  • 1




    The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
    – Paulpro
    Oct 19 '16 at 16:20












up vote
52
down vote



accepted







up vote
52
down vote



accepted






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 22 '11 at 20:55









Paulpro

111k15221229




111k15221229







  • 24




    Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
    – Chris Tonkinson
    Dec 22 '11 at 20:59










  • How is this working? The output is: Out[25]: ['iPhone']
    – lapinkoira
    Oct 19 '16 at 13:47






  • 6




    @lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
    – Paulpro
    Oct 19 '16 at 14:55







  • 4




    @lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
    – Paulpro
    Oct 19 '16 at 16:14







  • 1




    The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
    – Paulpro
    Oct 19 '16 at 16:20












  • 24




    Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
    – Chris Tonkinson
    Dec 22 '11 at 20:59










  • How is this working? The output is: Out[25]: ['iPhone']
    – lapinkoira
    Oct 19 '16 at 13:47






  • 6




    @lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
    – Paulpro
    Oct 19 '16 at 14:55







  • 4




    @lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
    – Paulpro
    Oct 19 '16 at 16:14







  • 1




    The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
    – Paulpro
    Oct 19 '16 at 16:20







24




24




Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
– Chris Tonkinson
Dec 22 '11 at 20:59




Or even "Sent from my iP(:?hone|[ao]d)", which includes the iPad as well.
– Chris Tonkinson
Dec 22 '11 at 20:59












How is this working? The output is: Out[25]: ['iPhone']
– lapinkoira
Oct 19 '16 at 13:47




How is this working? The output is: Out[25]: ['iPhone']
– lapinkoira
Oct 19 '16 at 13:47




6




6




@lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
– Paulpro
Oct 19 '16 at 14:55





@lapinkoira The output of what? Do you really think this warrants a down vote when it works for the person who asked the question and everyone else who found it, but doesn't work for a specific way that you're using it that has nothing to do with the original question?
– Paulpro
Oct 19 '16 at 14:55





4




4




@lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
– Paulpro
Oct 19 '16 at 16:14





@lapinkoira My answer wouldn't match the strings iPhone or iPod on there own. It will only match Sent from my iPhone or Sent from my iPod, just like the OP wants. There is a difference between what is matched and what is captured, and the OP only asked about what is matched. re.compile("(iPhone|iPod)") would find a match in the string Red iPhone, but my answer doesn't, since my answer requires the string to have Sent from my before either iPhone or iPod. Btw, regular expressions don't output anything, so I'm not sure what you mean by that.
– Paulpro
Oct 19 '16 at 16:14





1




1




The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
– Paulpro
Oct 19 '16 at 16:20




The other answer just suppresses capturing the group, so while both of our answers give match.group(0) == 'Sent from my iPhone', mine also allows you to get just the part that was captured separately if you want using match.group(1) == 'iPhone', whereas match.group(1) in the other answer would throw an IndexError: no such group.
– Paulpro
Oct 19 '16 at 16:20












up vote
21
down vote













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






share|improve this answer
















  • 3




    No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
    – PaulMcG
    Dec 22 '11 at 23:29










  • This should be the accepted answer
    – lapinkoira
    Oct 19 '16 at 14:17














up vote
21
down vote













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






share|improve this answer
















  • 3




    No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
    – PaulMcG
    Dec 22 '11 at 23:29










  • This should be the accepted answer
    – lapinkoira
    Oct 19 '16 at 14:17












up vote
21
down vote










up vote
21
down vote









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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 22 '11 at 20:57









ThiefMaster

235k60459554




235k60459554







  • 3




    No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
    – PaulMcG
    Dec 22 '11 at 23:29










  • This should be the accepted answer
    – lapinkoira
    Oct 19 '16 at 14:17












  • 3




    No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
    – PaulMcG
    Dec 22 '11 at 23:29










  • This should be the accepted answer
    – lapinkoira
    Oct 19 '16 at 14:17







3




3




No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
– PaulMcG
Dec 22 '11 at 23:29




No they are testing for "Sent from my " and then one of the characters i, P, h, o, n, or e; OR one of the characters i, P, o, or d. The vertical bar for OR is topmost scope, and the left hand side has "Sent from my" in it but the right hand side does not.
– PaulMcG
Dec 22 '11 at 23:29












This should be the accepted answer
– lapinkoira
Oct 19 '16 at 14:17




This should be the accepted answer
– lapinkoira
Oct 19 '16 at 14:17

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8609597%2fpython-regular-expressions-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20