您的位置:首页 > 其它

正则表达式匹配多行

2016-03-14 16:01 375 查看
a.*?b
匹配
最短的,以a开始,以b结束的字符串
。如果把它应用于

aabab
的话,它会匹配
aab(第一到第三个字符)

ab(第四到第五个字符)

#Shell 脚本测试实例
# re.S means: Make the '.' special charactermatch any character at all,
# including a newline; without this flag,'.' will match anything except a newline.
# '(.+?)' means: this is a lazzy match.When the fist 'Response>' is found, then
# it will not try to match the next'Response>'
#错误表达式
#re_patt = re.compile(r'<ResponseStatus="OKAY" CongLvl="LEVEL0"*(.+?)Response>', re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">\w*\n<ResponseStatus="OKAY"*(.+?)Response>', re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">.*\n<ResponseStatus="OKAY".*RequestId="100000">', re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">*(.+?)\n<ResponseStatus="OKAY"*(.+?)RequestId="100000">')
#re_patt = re.compile(r'<RequestAction="READ"*(.+?)<ResponseStatus="OKAY"*(.+?)RequestId="100000">', re.S)
#re_patt = re.compile(r'<RequestAction="READ"*(.+?)<Response Status="OKAY"RequestId="100000">', re.S)
#re_patt = re.compile(r'<ResponseStatus="OKAY" CongLvl="LEVEL0"*(.+?)Response>', re.S)
#re_patt = re.compile(r'<RequestAction="READ"RequestId="100000">*(.+?)encoding="UTF-8"?>', re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">\w*', re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">*(.+?)Response>',re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000".*')
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000"*(.+?)Response>', re.S)
#re_patt = re.compile(r'<RequestAction="LOGIN"RequestId="100000"><Authentication><ClientName>Administrator</ClientName><Password>5420!Gls</Password></Authentication></Request><?xmlversion="1.0"
encoding="UTF-8"?>*(.+?)Response>',re.S)
#re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">.*\n<ResponseStatus="OKAY"*(.+?)Response>', re.S)
#正确表达式
re_patt = re.compile(r'<RequestAction="READ" RequestId="100000">.*?<ResponseStatus="OKAY".*?Response>', re.S)

str1 = ""
# save the readout lines into str1.
for line in read_file:
str1 = str1 + line

print str1
result = re_patt.search(str1)
if result != None:
print 'test2'
tempResult1 = '<ResponseBatch>' +result.group(0) + '</ResponseBatch>'
newfile.write(tempResult1)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: