Veritas
Veritas

Reputation: 2210

Python re.findall() and precedence

How can I use the re.findall() function to retrieve a list of match instances when I use nested parentheses in the regex pattern to account for precedence?

Example:

import re
string = 'blah blah 12234 (12) (23) (34)'
print re.findall(r'\((\d)+)\)', string)

This prints:

  [('12', '2'), ('23', '3'), ('34', '4')]

Instead of the expected

  ['12', '23', '34']

Of course in this case this can be fixed by simply changing the inner parentheses:

print re.findall(r'\((\d+)\)', string)

Is there any way to explicitly handle precedence without affecting the result?

Upvotes: 2

Views: 1432

Answers (3)

6502
6502

Reputation: 114569

You can say that the external parenthesis are verbatim parenthesis chars to be matched

>>> print re.findall(r'\((\d+)\)', s)
['12', '23', '34']
>>>

If instead you're talking about matching an expression only if it's inside a balanced set of parenthesis then I've bad news for you: the regular expression language is not powerful enough for that (because that kind of grammar is not "regular"). It's not a Python implementation problem... it's a problem that cannot be solved even in theory with regexps (in other words it's not that we don't know how to solve it... we know that it cannot be solved).

You need to write explicit Python code to parse arbitrarily nested grammars (if you care about the nesting level). Regular expressions are DFSAs not able to "count" the levels; that requires memory and at least a PDA.

Upvotes: 4

Avinash Raj
Avinash Raj

Reputation: 174796

You don't need to capture anything. Just Use a postive lookbehind and look-ahead in your regex,

>>> import re
>>> string = 'blah blah 12234 (12) (23) (34)'
>>> print re.findall(r'(?<=\()\d+(?=\))', string)
['12', '23', '34']

You need to put + inside the capturing group.

>>> print re.findall(r'\((\d+)\)', string)
['12', '23', '34']

Upvotes: 3

vks
vks

Reputation: 67988

\((\d+)\)

This works.See demo.

http://regex101.com/r/wE3dU7/2

Upvotes: 0

Related Questions