Repeating a regex section?

I need to parse a text dump of a spreadsheet. I have a regex that parses every row of data correctly, but is pretty long. This basically just matches a specific pattern 12 or 13 times.

The pattern I want to repeat

\s+(\w*\.*\w*); 

This is a regex (abbreviated)

 ^\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*);\s+(\w*\.*\w*); 

Is there a way to match a pattern with a given number of times without copying the paste like that? Each of these sections corresponds to data columns, all of which I need. By the way, I am using Python. Thanks!

+4
source share
2 answers

How about using:

 (\s+(\w*\.*\w*);)* 

Have you found the findall method yet? Or consider a partition into ; ?

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

probably what you really want.

+3
source

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

{n} is "repeating n times"

if you want "12 - 13" times,

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

if you want "12+" times,

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

+30
source

Source: https://habr.com/ru/post/1390734/


All Articles