次の構文を説明するヘルプを探しています。私は Perl の本をチェックし、オンラインでチェックしました。
私は次のものを探しています:
$option =~ s/\s+//g
$option =~ m/^\s*$
どうもありがとう。
sおよびは、文字列に正規表現mを適用する演算子です。は置換演算子であり、一致演算子です。どちらも最初の引数 ( 内) として正規表現を取ります。は、Perl に対して正規表現を使用/照合するように指示します。詳細については、を参照してください。s///m////=~$optionperlre
これらの正規表現の機能は次のとおりです。
use warnings;
use strict;
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new( qr/\s+/ )->explain;
The regular expression:
(?-imsx:\s+)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
要するに、複数の空白文字を含む空白文字のすべてのインスタンスを何も置き換えません。
と:
print YAPE::Regex::Explain->new( qr/^\s*$/ )->explain;
正規表現:
(?-imsx:^\s*$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
そして、文字列に空白文字のみが含まれている場合、それは一致します。できるだけ多く、ただし少なくともゼロで、他には何もありません。
最初の式は、s1 つ以上の空白を探して削除する (置換) です。は、+1 つ以上の空白 ( \s) 文字を指定します。
m2 番目は、空の行を検索(一致) します。^アンカーは、行の先頭と末尾に一致します$。したがって、0 個以上 (つまり*) の空白を含む行のみが正常に照合されます。
式は変数にバインドされているため、どちらの式も$option変数に対して動作します。=~