adele adele - 1 year ago 68
Python Question

Capture group with python regex not capturing

Im trying to gain an understanding of capture groups using this example:

sentence = "the quick brown fox jumps over the lazy dog"'\S+\s+\S+',sentence)

<_sre.SRE_Match object; span=(0, 9), match='the quick'>

I can see this matches as follows:'\S+\s+\S+',sentence).group()

'the quick'

I want to add a match group for the word 'quick' so I try this:'\S+\s+\(S+)',sentence)

Which gives an error:

error: unbalanced parenthesis at position 10

What am I doing wrong here?

Answer Source

Looks like a typo, but I'll still provide an explanation.

You are escaping the opening parenthesis making it matching a literal (, which makes the closing parenthesis at the end of the expression without an opening part, replace: