5

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 ?

1
  • 1
    Just edited my question, your answer is valid solution Commented Mar 2, 2018 at 21:49

1 Answer 1

7

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')
Sign up to request clarification or add additional context in comments.

3 Comments

Or, heck, just remove the groups entirely.
is it needed to escape single quotes inside double quotes? re.findall(r"'[^']+'|0x\w+",s) should work too
@Sundeep correct, I was using single quote at first, got annoyed by this, but forgot to remove escape

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.