如何用perl中的引号字符串?

时间:2011-07-03 14:43:18

标签: regex perl

my $search = "Sony's";
my $string = "in TV sony's walkman";

如何使用正则表达式在sony's中获得“$string”匹配。

2 个答案:

答案 0 :(得分:6)

my $search = "Sony's";
my $string = "in TV sony's walkman";

$string =~ /$search/;   # does not match
$string =~ /$search/i;  # matches case-insensitive

这是一个初学者问题。请先learn the languageread a book or two

答案 1 :(得分:3)

由于您需要不区分大小写的匹配项,请使用i修饰符:

#!/usr/bin/env perl

use strict;
use warnings;

my $search = "Sony's";
my $string = "in TV sony's walkman";

if ( my ($match) = $string =~ /($search)/i ) {
    print "Matched string: $match";
}

请阅读:perldoc perlretut