可以有人告诉我它到底要匹配什么吗?
$exp = '/[\s]+col[\s]*=[\s]*"([^"]*)"/si';
如果你添加了/x
修饰符,你可以使用注释编写正则表达式。因此,这里是一个冗长且有文档说明的版本(对于复杂的正则表达式总是建议这样做):
$exp = '/
[\s]+ # one or more spaces
col # col
[\s]* # zero or more spaces
= # =
[\s]* # spaces
" # "
([^"]*) # anything but " and zero or more of it
" # "
/six';
[^<">]
代替 [^"]
,这样的正则表达式能够更加容忍格式不正确的html。看起来匹配的是 col="some value"
,对等号周围的空格非常宽容,不区分大小写,无论值是否为空。
顺便说一句,有趣的是s
修饰符在这里有什么作用,因为没有.
元字符。
COL="<value>"
匹配。 - moinudinuse strict;
use warnings;
my $regex = qr/
\s+ col \s* = \s* "( (?: \\. | [^\\"]+ )* )"
/sx;
my $string = q(
col = " this'' is \" a test\s,
of the emergency broadcast system,
alright .\". cool."
);
if ( $string =~ /$regex/ )
{
print "Passed val =\n $1\n";
}
__END__
Passed val =
this'' is \" a test\s,
of the emergency broadcast system,
alright .\". cool.
/\scol\s*=\s*"([^"]*)"/i
。 - moinudin