正则表达式实现跨行匹配

正则表达式(Regular expression)可用来检查文本中是否包含指定模式的字符串,通常是按行来处理(POSIX标准),因为.操作符通常不匹配换行符,如果要匹配多行怎么处理呢?本文介绍正则表达式跨行匹配实现方法 。
 
1. sed 命令删除多行测试文档test.txt内容如下:
starttest1test2end删除 start 和 end 之间的内容
# 包括`start` 和 `end`sed -i '/start/,/end/d' test.txt# 不包括`start` 和 `end`sed -i '/start/,/end/{{//!d;};}' test.txt 2. Python正则表达式匹配多行【正则表达式实现跨行匹配】Python中匹配多行方法如下:
①re.DOTALL或者re.S参数import redata = https://www.isolves.com/it/cxkf/sf/2022-03-22/"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']② 表达式(.|n|r)*import redata = https://www.isolves.com/it/cxkf/sf/2022-03-22/"1nstartntest1ntest2nendn2"reg3 = r"start((.|n|r)*)end"res = re.findall(reg3, data)print(res)执行结果:
[('ntest1ntest2n', 'n')]③ 表达式[sS]*import redata = https://www.isolves.com/it/cxkf/sf/2022-03-22/"1nstartntest1ntest2nendn2"reg4 = r"start([sS]*)end"res = re.findall(reg4, data)print(res)执行结果:
['ntest1ntest2n']④ 表达式(?s)import redata = https://www.isolves.com/it/cxkf/sf/2022-03-22/"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']参考:

  1. https://stackoverflow.com/questions/159118/how-do-i-match-any-character-across-multiple-lines-in-a-regular-expression
--THE END--




    推荐阅读