0

My problem is that this simple regex statement with a boolean operator only gives me the result I want when the first item on the left side of the bitwise operator | is present in the sentence. Could someone tell me why it isn't working on the alternative as well?

 import re

 b = 'this is a good day to die hard'

 jeff = re.search('good night (.+)hard|good day (.+)hard', b)

 print jeff.group(1)
2
  • Because the second (...) creates the second group. Commented Apr 3, 2017 at 21:41
  • The proper name for | is in a regular expression is "alternation." It's not really Boolean. Commented Apr 3, 2017 at 21:56

2 Answers 2

1

You have two sets of capturing parentheses - therefore you have two numbered capturing groups. If the second branch matches, the group(1) will be set to None, and group(2) will contain that which was matched by the second group.


There are several ways to fix this. One would be to write so that there is just one group, for example

jeff = re.search('good (?:day|night) (.+)hard', b)
Sign up to request clarification or add additional context in comments.

Comments

0

The second (...) creates the second capturing group that you need to access with .group(2).

You may write a regex that will capture day or night, and the second group will fetch all up to the last hard.

import re
b = 'this is a good day to die hard'
jeff = re.search('good (day|night) (.+)', b)
if jeff:
    print jeff.group(1)
    print jeff.group(2)

Output of the demo:

day
to die hard

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.