adele adele - 3 months ago 23
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"

re.search(r'\S+\s+\S+',sentence)

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


I can see this matches as follows:

re.search(r'\S+\s+\S+',sentence).group()

'the quick'


I want to add a match group for the word 'quick' so I try this:

re.search(r'\S+\s+\(S+)',sentence)


Which gives an error:

error: unbalanced parenthesis at position 10


What am I doing wrong here?

Answer

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:

\S+\s+\(S+)

with:

\S+\s+(\S+)