27 lines
799 B
Perl
27 lines
799 B
Perl
|
#!/usr/bin/perl
|
||
|
|
||
|
$ans="cat sheep tiger";
|
||
|
$ans =~ /(\w) *(\w) *(\w)/; # 正常情况下的匹配表达式
|
||
|
print "===match start===\n$&\n===match end===\n";
|
||
|
|
||
|
$ans="cat sheep tiger";
|
||
|
$ans =~ /(\w)\s* (\w)\s* (\w)/x;
|
||
|
print "===match start===\n$&\n===match end===\n";
|
||
|
|
||
|
$ans="cat sheep tiger";
|
||
|
$ans = ~ /
|
||
|
(\w)\s* # 可以加上本行注释:匹配第一个单词
|
||
|
(\w)\s* # 可以加上本行注释:匹配第二个单词
|
||
|
(\w) # 可以加上本行注释:匹配第三个单词
|
||
|
/x;
|
||
|
print "===match start===\n$&\n===match end===\n";
|
||
|
|
||
|
$ans="cat sheep tiger";
|
||
|
$ans =~ /
|
||
|
(\w)\Q \E # \Q \E强制将中间的空格当作字面符号被匹配
|
||
|
(\w)\Q \E
|
||
|
(\w)
|
||
|
/x;
|
||
|
print "===match start===\n$&\n===match end===\n";
|
||
|
|