repeating a section of a regular expression?


Question

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 matching a certain pattern 12 or 13 times.

The pattern I want to repeat is

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

This is the regular expression (shortened)

^\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 a set number of times without copy pasting like this? Each of those sections correspond to data columns, all of which I need. I'm using Python by the way. Thanks!

1
14
1/12/2012 10:38:41 PM

Accepted Answer

How about using:

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

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

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

is probably what you really want.

6
1/12/2012 10:41:11 PM

(\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,}


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon