正規表現を使ってPerlでスペースをマッチさせる方法は?
Perlでは、正規表現で空白にマッチする方法として以下のものが挙げられる。
- 彼は職場で昇進した。
my $string = "Hello world!";
if ($string =~ /\s/) {
print "Contains whitespace";
} else {
print "Does not contain whitespace";
}
- 会話の自然さに欠けます。
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";
}
具体的な要求に応じて上記の方法を選択して使用でき、正規表現のルールに従ってさらに複雑な空間に対応するパターンマッチングを使用することもできます。