用于perl字符串模式匹配的负正则表达式

前端之家收集整理的这篇文章主要介绍了用于perl字符串模式匹配的负正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个正则表达式:
if($string =~ m/^(Clinton|[^Bush]|Reagan)/i)
  {print "$string\n"};

我想和克林顿和里根,但不是布什。@H_502_5@

它不工作。@H_502_5@

解决方法

示例文本:

Clinton said
Bush used Crayons
Reagan forgot@H_502_5@

只是省略布什比赛:@H_502_5@

$ perl -ne 'print if /^(Clinton|Reagan)/' textfile
Clinton said
Reagan forgot

或者如果你真的想指定:@H_502_5@

$ perl -ne 'print if /^(?!Bush)(Clinton|Reagan)/' textfile
Clinton said
Reagan forgot
原文链接:https://www.f2er.com/Perl/173314.html

猜你在找的Perl相关文章