Matching sequentially repeating parentheses with Python Regex

Basically I'm trying to find a series of sequential repetitive patterns using python with a regex:

(X[0-9]+)+

      

For example, enter the input line:

YYYX4X5Z3X2

      

Get a list of results:

["X4X5", "X2"]

      

However, I get instead:

["X5", "X2"]

      

I checked the regexpal on regexpal and confirmed that it is correct, due to the way python handles the "()" I cannot get the desired result. Can anyone advise?

+3


source to share


3 answers


Turn your capturing group into a non-capturing group, (?:...)

not ...

>>> import re
>>> re.findall(r'(?:X[0-9]+)+', 'YYYX4X5Z3X2')
['X4X5', 'X2']

      



Another example:

>>> re.findall(r'(?:X[0-9]+)+', 'YYYX4X5Z3X2Z4X6X7X8Z5X9')
['X4X5', 'X2', 'X6X7X8', 'X9']

      

+4


source


change your template this way

((?:X[0-9]+)+)

      



Demo

(               # Capturing Group (1)
  (?:           # Non Capturing Group
    X           # "X"
    [0-9]       # Character Class [0-9]
    +           # (one or more)(greedy)
  )             # End of Non Capturing Group
  +             # (one or more)(greedy)
)               # End of Capturing Group (1)

      

+3


source


You need to specify in the group not capturing (?:<pattern>)

for the first template:

((?:X[0-9]+)+)

      

+3


source







All Articles