正则表达式(Regular expression)可用来检查文本中是否包含指定模式的字符串,通常是按行来处理(POSIX标准),因为.操作符通常不匹配换行符,如果要匹配多行怎么处理呢?本文介绍正则表达式跨行匹配实现方法。
测试文档test.txt内容如下:
start
test1
test2
end
删除 start 和 end 之间的内容
# 包括`start` 和 `end`
sed -i '/start/,/end/d' test.txt
# 不包括`start` 和 `end`
sed -i '/start/,/end/{{//!d;};}' test.txt
Python中匹配多行方法如下:
import re
data = "1nstartntest1ntest2nendn2"
reg1 = r"start.*end"
reg2 = r"start(.*)end"
res1 = re.findall(reg1, data, flags=re.S)
print(res1)
res2 = re.findall(reg2, data, flags=re.DOTALL)
print(res2)
执行结果:
['startntest1ntest2nend']
['ntest1ntest2n']
import re
data = "1nstartntest1ntest2nendn2"
reg3 = r"start((.|n|r)*)end"
res = re.findall(reg3, data)
print(res)
执行结果:
[('ntest1ntest2n', 'n')]
import re
data = "1nstartntest1ntest2nendn2"
reg4 = r"start([sS]*)end"
res = re.findall(reg4, data)
print(res)
执行结果:
['ntest1ntest2n']
import re
data = "1nstartntest1ntest2nendn2"
reg5 = r"(?s)start(.*)end"
res = re.findall(reg5, data)
print(res)
reg5 = r"(?s)start.*end"
res = re.findall(reg5, data)
print(res)
执行结果:
['ntest1ntest2n']
['startntest1ntest2nend']
参考:
--THE END--