Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repeating a section of a regular expression?

Tags:

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!

like image 571
Joe Lyga Avatar asked Jan 12 '12 22:01

Joe Lyga


People also ask

How do you repeat a regular expression?

An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once. An expression followed by '? ' may be repeated zero or one times only.

What is '?' In regular expression?

means "zero or one digits, but not two or more". [0-9]* means "zero or more digits (no limit, could be 42 of them)". Note that some languages require that floats are written with a leading 0 before the .

Why * is used in regex?

- a "dot" indicates any character. * - means "0 or more instances of the preceding regex token"

What is quantifier in regex?

Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.


1 Answers

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

like image 59
joe_coolish Avatar answered Oct 14 '22 00:10

joe_coolish