george mano
george mano

Reputation: 6168

Regular Expression for multiple lines

I have made this regular expression ((\s\s\s.*\s)*) . It means , 3 spaces or tabs , after that a number of characters and a space or a tab or an end of line. How can I store to a list each line I get?

So , if I have something like this :

___The rabbit caught the lion\n
___The tiger got the giraffe\n
___The owl hit the man\n

the output to be like this :

list=[The rabbit caught the lion, The tiger got the giraffe, The owl hit the man]

Just to mention that I use groups for each of the other patterns that I have.

Upvotes: 1

Views: 302

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173562

It looks like you just want to match a whole line, starting with three spaces (or tabs); this can be done with re.MULTILINE and using the ^ and $ anchors.

matches = re.findall('^\s{3}(.*)$', contents, re.MULTILINE)

If you don't care about having characters before the three spaces,you can reduce the expression to this:

matches = re.findall('\s{3}(.*)', contents)

This is because . will match everything up to a newline (by default).

Upvotes: 1

Related Questions