加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在正则表达式中匹配“/ *”?

发布时间:2020-12-14 06:07:10 所属栏目:百科 来源:网络整理
导读:$stuff = "d:/learning/perl/tmp.txt";open STUFF,$stuff or die "Cannot open $stuff for read :$!";while (STUFF) { my($line) = $_; # Good practice to always strip the trailing chomp($line); my @values = split(' ',$line); foreach my $val (@valu
$stuff = "d:/learning/perl/tmp.txt";

open STUFF,$stuff or die "Cannot open $stuff for read :$!";
while (<STUFF>) {
    my($line) = $_; # Good practice to always strip the trailing
    chomp($line);
    my @values = split(' ',$line);

    foreach my $val (@values) {

        if ($val == 1){
            print "1 found";    
        }
        elsif ($val =~ /hello/){
            print "hello found";    
        }
        elsif ($val =~ /"/*"/){ # I don't know how to handle here.
            print "/* found";    
        }
        print "n";
    }
}

我的tmp.txt:

/* CheerStone ColdStunner 

1 Cheer Rock

hello Boo Pedigree

如何在代码中处理/ *字符序列?

解决方法

这两个字符在Perl正则表达式中都有特殊含义,因此您必须使用反斜杠转义它们:

$val =~ //*/

顺便说一下,你应该在所有正则表达式前面添加^,因为你似乎只想处理行开头的文本.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读