oaranger
oaranger

Reputation: 189

regex with findall multiple patterns in Python

Let's say I have a string s = "We are 'Champion' of 'The Team' if we got 0x12 to 0x34 correct answers"

I want to get a list ['Champion','The Team','0x12','0x34']

Here is what I have tried:

k = re.findall(r'(\'\w+\')|(0x\w+)',s)

but I get this instead

[("'Champion'", ''), ('', '0x12'), ('', '0x34')]

How do I fix my code ?

Upvotes: 5

Views: 10639

Answers (1)

Olivier Melançon
Olivier Melançon

Reputation: 22294

This is because (...) is a capturing group, it makes your match return a tuple containing the strings that matched the sub-regexs between (...).

You can use (?:...) instead for your group to be non-capturing. Or in this case, you can just remove parentheses altogether.

re.findall(r"'[^']+'|0x\w+",s)
# ["'Champion'", "'The Team'", '0x12', '0x34']

Note that if you expect a fixed number of match, you could actually use capturing group to solve your problem.

re.match(r".*?'([^']+)'.*?'([^']+)'.*?(0x\w+).*?(0x\w+)",s).groups()
# ("Champion", "The Team", '0x12', '0x34')

Upvotes: 7

Related Questions