repeating a section of a regular expression?

前端 未结 2 692
死守一世寂寞
死守一世寂寞 2020-12-20 11:26

I\'m having to parse a text dump of a spreadsheet. I have a regular expression that correctly parses each line of the data, but it\'s rather long. It\'s basically just mat

相关标签:
2条回答
  • 2020-12-20 12:01

    How about using:

    [x.group() for x in re.finditer(r'(\s+(\w*\.*\w*);)*', text)]
    

    Did you find the findall method yet? Or consider splitting at ;?

    map(lambda x: x.strip(), s.split(";"))
    

    is probably what you really want.

    0 讨论(0)
  • 2020-12-20 12:08

    (\s+(\w*\.*\w*);){12}

    The {n} is a "repeat n times"

    if you want "12 - 13" times,

    (\s+(\w*\.*\w*);){12,13}

    if you want "12+" times,

    (\s+(\w*\.*\w*);){12,}

    0 讨论(0)
提交回复
热议问题