如何将perl中的两个长字符串与特殊字符匹配

时间:2015-09-28 15:43:07

标签: perl

我想比较两个字符串是否相等。

      $txt  = 'Label Only - if no signs of leakage.';
      $txt1 = 'Label Only - if no signs of leakage';

      if($txt eq $txt1)
      {
        print "Both are equal";
      }

但由于$ txt1不包含句号或点数,因此无法匹配。可以请指导我如何逃避句点,以便变量$ txt和$ txt1相等。

提前致谢

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你想检查这两个字符串是否相等,或者仅仅是一个尾随句点。

$str1 eq $str2 || "$str1." eq $str2 || $str1 eq "$str2."

( $str1 =~ s/\.\z//r ) eq ( $str2 =~ s/\.\z//r )   # 5.14+

答案 1 :(得分:0)

下面在比较之前使用$txt正则表达式修饰符非破坏性地删除/r中的跟踪点。此功能是在perl v5.14中引入的。

use warnings;
use strict;

my $txt  = 'Label Only - if no signs of leakage.';
my $txt1 = 'Label Only - if no signs of leakage';

if($txt =~ s/\.$//r eq $txt1){
    print "Both are equal";
}

更新:已修复仅剥离尾随期间。

相关问题