0

I am using python's re module and I would like to search and capture one of two possible items, but I can't figure out how to get the grouping and non capturing grouping to work right:

Lets say my pattern is:

(?:(test)55)|(apple)

So basically I want to capture 'test' or 'apple' but only capture 'test' if it is followed by 55.

The problem here is that I end up with 2 capture groups so I end up with either (None, 'apple') or ('test', None).

I know that I can just post-process it:

next( item for item in groups() if item is not None)

but I was wondering if there is a way to phrase it in the regex that would make it work (I just want to learn regex better).

Thanks.

2
  • You get two because (test) is a capturing group, too. Commented Apr 8, 2016 at 23:52
  • A Match Object has a lastindex attribute that returns the index of the last capturing group that matched something. If the capture groups are mutually exclusive, like in your example, your post processing could be simplified to matchobject.group(matchobject.lastindex) Commented Apr 9, 2016 at 3:54

1 Answer 1

1

To capture only one group, use just one group of capturing parentheses containing (test|apple). Add a lookahead so that test must be followed by 55:

re.findall(r"(test(?=55)|apple)", mystring);
Sign up to request clarification or add additional context in comments.

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.