how to match lines between pattern using re module in python
I have a string (with multiple lines) which contains the following:
"Name=My name
Address=......
\##### To ext开发者_Go百科ract from here ####
line 1
line 2
line 3
\##### To extract till here ####
close"
How do I extract the lines between "##### To extract *"
string including the pattern as well?
Output should be the following:
\##### To extract from here ####
line 1
line 2
line 3
pat = re.compile('\\\\##### To extract from here ####'
'.*?'
'(?=\\\\##### To extract till here ####)',
re.DOTALL)
or
pat = re.compile(r'\\##### To extract from here ####'
'.*?'
r'(?=\\##### To extract till here ####)',
re.DOTALL)
Ofir is right. Here's a corresponding example:
>>> s = """... your example string ..."""
>>> marker1 = "\##### To extract from here ####"
>>> marker2 = "\##### To extract till here ####"
>>> a = s.find(marker1)
>>> b = s.find(marker2, a + len(marker1))
>>> print s[a:b]
\##### To extract from here ####
line 1
line 2
line 3
You don't need regular expressions for that, a simple string.find would suffice.
Simply find both strings, and output the portion of the input between them (by slicing the string), taking care to avoid outputting the first string (i.e. noting its length).
Alternatively, you can use two calls to string.split.
>>> s
'\nName=My name\nAddress=......\n\\##### To extract from here ####\nline 1\nline 2\nline 3\n\\##### To extract till here ####\nclose'
>>> for o in s.split("\n"):
... if "##" in o and not flag:
... flag=1
... continue
... if flag and not "##" in o:
... print o
... if "##" in o and flag:
... flag=0
... continue
...
line 1
line 2
line 3
精彩评论