我想找到一个正则表达式或 perl 单行代码来检测两行连续的行,这些行包含相同的单词且顺序相同,但前导空格、尾随空格和单词之间的空格数量可能因行而异。正则表达式必须排除那些不匹配的行。
我使用这个正则表达式来检测相同的连续行,
^\s*(.*\S)(\s\n*\1)+$
当一行与另一行之间的空格量不同时,它会失败。
我有这个示例文本
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
vrfgrgru hfiughf iujhfuihgifughuir nuhuvhfug
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
fghurhg bojghirejyohi ndhguhyituhgi
gbshbsiughsf bnighriugh hurhgiurhgriu
Elvis was the greatest singer of all time. He has 18 number one in billboard charts.
Elvis was the greatest singer of all time. He has 18 number one in billboard charts.
sgfuhgdugusd vfjsng orjgoergo ejfio dsmnjn
fbdhsbfgdbhhbfhdsfdf
Mary has a little lamb. The live near the mountain.
Mary has a little lamb. The live near the mountain.
gsete gerttrb oijoijoijoijoijoiojo gogoerijoi
jhoiyrei erogjrijgtrio noifdo mkooi
This has been the coldest winter in the last 20 years.
This has been the coldest winter in the last 20 years.
无论行中有多少个空格,正则表达式都应该显示包含相同信息的连续行。
没有三个连续的并且匹配的行之间没有空行。
嗯,它不是一个正则表达式,也不是一个 perl 单行程序,但是我认为你可以在每个 Unix 机器上的任何 shell 中使用任何 awk 来做你真正想做的事情:
我假设您喜欢简洁,因为您要求使用正则表达式或 perl 单行代码,但如果您更喜欢易读性,这里再次提供更有意义的变量名和更好的空格: