Python regular expressions OR
up vote
28
down vote
favorite
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
add a comment |
up vote
28
down vote
favorite
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
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
add a comment |
up vote
28
down vote
favorite
up vote
28
down vote
favorite
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
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
python regex
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
up vote
52
down vote
accepted
re.compile("Sent from my (iPhone|iPod)")
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 stringsiPhone
oriPod
on there own. It will only matchSent from my iPhone
orSent 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 stringRed iPhone
, but my answer doesn't, since my answer requires the string to haveSent from my
before eitheriPhone
oriPod
. 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 givematch.group(0) == 'Sent from my iPhone'
, mine also allows you to get just the part that was captured separately if you want usingmatch.group(1) == 'iPhone'
, whereasmatch.group(1)
in the other answer would throw anIndexError: no such group
.
– Paulpro
Oct 19 '16 at 16:20
|
show 1 more comment
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..
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
add a comment |
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)")
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 stringsiPhone
oriPod
on there own. It will only matchSent from my iPhone
orSent 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 stringRed iPhone
, but my answer doesn't, since my answer requires the string to haveSent from my
before eitheriPhone
oriPod
. 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 givematch.group(0) == 'Sent from my iPhone'
, mine also allows you to get just the part that was captured separately if you want usingmatch.group(1) == 'iPhone'
, whereasmatch.group(1)
in the other answer would throw anIndexError: no such group
.
– Paulpro
Oct 19 '16 at 16:20
|
show 1 more comment
up vote
52
down vote
accepted
re.compile("Sent from my (iPhone|iPod)")
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 stringsiPhone
oriPod
on there own. It will only matchSent from my iPhone
orSent 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 stringRed iPhone
, but my answer doesn't, since my answer requires the string to haveSent from my
before eitheriPhone
oriPod
. 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 givematch.group(0) == 'Sent from my iPhone'
, mine also allows you to get just the part that was captured separately if you want usingmatch.group(1) == 'iPhone'
, whereasmatch.group(1)
in the other answer would throw anIndexError: no such group
.
– Paulpro
Oct 19 '16 at 16:20
|
show 1 more comment
up vote
52
down vote
accepted
up vote
52
down vote
accepted
re.compile("Sent from my (iPhone|iPod)")
re.compile("Sent from my (iPhone|iPod)")
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 stringsiPhone
oriPod
on there own. It will only matchSent from my iPhone
orSent 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 stringRed iPhone
, but my answer doesn't, since my answer requires the string to haveSent from my
before eitheriPhone
oriPod
. 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 givematch.group(0) == 'Sent from my iPhone'
, mine also allows you to get just the part that was captured separately if you want usingmatch.group(1) == 'iPhone'
, whereasmatch.group(1)
in the other answer would throw anIndexError: no such group
.
– Paulpro
Oct 19 '16 at 16:20
|
show 1 more comment
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 stringsiPhone
oriPod
on there own. It will only matchSent from my iPhone
orSent 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 stringRed iPhone
, but my answer doesn't, since my answer requires the string to haveSent from my
before eitheriPhone
oriPod
. 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 givematch.group(0) == 'Sent from my iPhone'
, mine also allows you to get just the part that was captured separately if you want usingmatch.group(1) == 'iPhone'
, whereasmatch.group(1)
in the other answer would throw anIndexError: 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
|
show 1 more comment
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..
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
add a comment |
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..
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
add a comment |
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..
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 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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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