Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex, matching pattern over multiple lines.. why isn't this working?

I know that for parsing I should ideally remove all spaces and linebreaks but I was just doing this as a quick fix for something I was trying and I can't figure out why its not working.. I have wrapped different areas of text in my document with the wrappers like "####1" and am trying to parse based on this but its just not working no matter what I try, I think I am using multiline correctly.. any advice is appreciated

This returns no results at all:

string='
####1
ttteest
####1
ttttteeeestt

####2   

ttest
####2'

import re
pattern = '.*?####(.*?)####'
returnmatch = re.compile(pattern, re.MULTILINE).findall(string)
return returnmatch
like image 810
Rick Avatar asked Aug 20 '10 20:08

Rick


People also ask

What is multiline pattern match?

Pattern. MULTILINE or (? m) tells Java to accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string). Pattern.

How do I match a pattern in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is pattern in regex python?

A Regular Expression (RegEx) is a sequence of characters that defines a search pattern. For example, ^a...s$ The above code defines a RegEx pattern. The pattern is: any five letter string starting with a and ending with s . A pattern defined using RegEx can be used to match against a string.


1 Answers

Multiline doesn't mean . will match line return, it means that ^ and $ are limited to lines only

re.M re.MULTILINE

When specified, the pattern character '^' matches at the beginning of the string and at the >beginning of each line (immediately following each newline); and the pattern character '$' >matches at the end of the string and at the end of each line (immediately preceding each >newline). By default, '^' matches only at the beginning of the string, and '$' only at the >end of the string and immediately before the newline (if any) at the end of the string.

re.S or re.DOTALL makes . match even new lines.

Source

http://docs.python.org/

like image 153
Colin Hebert Avatar answered Sep 28 '22 12:09

Colin Hebert