Search code examples
pythonregexregex-group

Regex: How to repeat group area?


How can I repeat a certain part of a match area without writing it multiple times?

For example:

txt = '1. Reserve December 31, prior year.................................................................................................................. ..4,587,658,997 .......................... .1,030,275,014 .....136,963,988 .......................... .3,276,184,545 .....144,235,450 .......................... .......................... .......................... .......................... ..........................'

splitter = '^([\d.]+)(.*?)\.\s\.[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?[\.\s]+(\(*\d[\d,.]*\)*)?'

parts = re.match(splitter, x, re.DOTALL)

The first part of my regex expression, ^([\d.]+)(.*?)\.\s\. grabs the row number, and title:

  1. Reserve December 31, prior year

after that I have to repeat this section 12 times to ensure I get 12 additional matches for the numbers after the title [\.\s]+(\(*\d[\d,.]*\)*)?.

If there aren't 12 numbers it returns None for that specific match.

Is there a way I can repeat this expression 12 times without having to write such a long regex expression? I tried (?:[\.\s]+(\(*\d[\d,.]*\)*)?){12} but no dice.


Solution

  • I think you just need to put your second part into a group by putting () around it and then putting your exact count outside of the group like this:

    checkIt = re.compile(r'^([\d.]+)(.*?)\.\s\.([\.\s]+(\(*\d[\d,.]*\)*)?){12}')
    if checkIt.match(text):
        do something