在Perl中,正则表达式匹配空格的方法有以下几种:
\s
来匹配任意空白字符,包括空格、制表符和换行符。my $string = "Hello world!";
if ($string =~ /\s/) {
print "Contains whitespace";
} else {
print "Does not contain whitespace";
}
\s
来匹配一个空格字符。my $string = "Hello world!";
if ($string =~ / /) {
print "Contains space";
} else {
print "Does not contain space";
}
" "
直接匹配一个空格字符。my $string = "Hello world!";
if ($string =~ / /) {
print "Contains space";
} else {
print "Does not contain space";
}
以上方法可以根据具体需求选择使用,根据正则表达式的规则,还可以使用更复杂的模式匹配空格。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:perl正则表达式匹配变量的方法是什么