繁体   English   中英

Perl 正则表达式与捕获组不工作

[英]Perl regex with capture groups not working

我有以下文件:

/Users/x/-acite _1660475490N.html
/Users/x/-acite _1660464772N.html
/Users/x/-acite _1660464242N.html
/Users/x/-acite _1660463321N.html
/Users/x/-acite _1660421444N.html
/Users/x/-acite _1612414441N.html

/Users/x/fix _1660399672N.html
/Users/x/fix _1660398829N.html

/Users/x/water witching _1660460617N.html
/Users/x/water witching _1660388149N.html
/Users/x/water witching _1632222441N.html
/Users/x/water witching _1660003224N.html

我需要

/Users/x/-acite _1660475490N.html
/Users/x/fix _1660399672N.html
/Users/x/water witching _1660460617N.html

我使用以下 perl 正则表达式:

find . -type f -exec perl -pi -w -e 's/(.*)(\R)(.*)(\R)/$1$2/' \{\} \;

或者

find . -type f -exec perl -pi -w -e 's/(.*?)(\R)(.*?)(\R)/$1$2/g;' \{\} \;

为什么这些不起作用?

此外,您可以在paragraph模式下阅读 ( -00 ),然后匹配并打印每个“段落”的第一行。

C:\Old_Data\perlp>perl -00 -ne 'print /(.+\n)/' test01.txt
/Users/x/-acite _1660475490N.html
/Users/x/fix _1660399672N.html
/Users/x/water witching _1660460617N.html

你是

  • 不要将整个文件啜饮成单个字符串,并且
  • 只替换第一次出现
  • 而且您不需要这么多组,您只需要一个,因为您想保留比赛的一部分。

你需要

find . -type f -exec perl -0777 -i -pe 's/^(.+)(?:\R.+)*\n/$1/gm' \{\} \;

这里,

  • -0777啜饮文件
  • ^ - 一行的开始(由于m标志)
  • (.+) - 匹配非空行
  • (?:\R.+)* - 零个或多个换行符和非空行序列
  • \n - 匹配换行符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM