正则表达式匹配行块

时间:2017-11-28 17:03:13

标签: regex

我有这样的档案:

Analysis of its root cause:
Blablablablabla
blabablabkjhjk
kjbsqbdqbds

Details of the fix
blablabla

Analysis of its root cause:
fddsfsdfsdfdsfs
blnskdbbqbbb
xxxxggggggg

Details of the fix
blablabla

分析其根本原因在文件中重复x次。我希望得到由"分析其根本原因"分隔的文本块。和"修复的详细信息"。

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:2)

我非常确定有更好的方法可以做到这一点,但那是我能管理的:

/(?(?<=Analysis of its root cause:\n)((.*\n)*)(?=Details of the fix\n))/gU

我使用正向前瞻和后视,以及以下修饰符:

g - global - 不要在第一场比赛后返回

u - Ungreedy - 让量词变得懒惰

在线试用:https://regex101.com/r/xpz7pg/2

答案 1 :(得分:1)

不是正则表达式的答案,而是使用perl

将您的行放入单个文件中。

 perl -e '$/="Analysis of its root cause:"; #Sets the record delimiter 

      while(<>){ #Iterates over the file, record by record

      chomp; #Removes the delimiter

      if ($_ =~ /\n(.*?)\nDetails of the fix\n(.*)\n/s){ #Matches strings between Details of the fix. . is allowed to match newline

      print "ONE:$1TWO:$2"} # $1 is the analysis, $2 is the details
      }'
      file.txt

输出

ONE:Blablablablabla
blabablabkjhjk
kjbsqbdqbds
TWO:blablabla
ONE:fddsfsdfsdfdsfs
blnskdbbqbbb
xxxxggggggg
TWO:blablabla