私はの基本的なルールを理解するのに多くの問題を抱えており、regex
誰かが「平易な英語」でそれらを説明するのを手伝ってくれることを願っています。
$_ = '1: A silly sentence (495,a) *BUT* one which will be useful. (3)';
print "Enter a regular expression: ";
my $pattern = <STDIN>;
chomp($pattern);
if (/$pattern/) {
print "The text matches the pattern '$pattern'.\n";
print "\$1 is '$1'\n" if defined $1;
print "\$2 is '$2'\n" if defined $2;
print "\$3 is '$3'\n" if defined $3;
print "\$4 is '$4'\n" if defined $4;
print "\$5 is '$5'\n" if defined $5;
}
3つのテスト出力
Enter a regular expression: ([a-z]+)
The text matches the pattern '([a-z]+)'
$1 is 'silly'
Enter a regular expression: (\w+)
The text matches the pattern '(\w+)'
$1 is '1'
Enter a regular expression: ([a-z]+)(.*)([a-z]+)
The text matches the pattern '([a-z]+)(.*)([a-z]+)'
$1 is 'silly'
$2 is " sentence (495,a) *BUT* one which will be usefu'
$3 is 'l'
私の混乱は次のとおりです
([a-z]+)
「小文字のアルファベットと1つ以上の繰り返し」を意味するのではありませんか?もしそうなら、「予定」も取り上げられるべきではありませんか?()が記憶に関するものであることに関係がない限り(つまり、「愚かな」は5文字の単語であるため、「will」は取り上げられませんが、「willx」は取り上げられますか??)(\w+)
「任意の単語と1つ以上の繰り返し」を意味するのではありませんか?もしそうなら、繰り返しがなくコロン「:」が後にあるので、なぜ数字「1」がピックアップされるのですか?([a-z]+)(.*)([a-z]+)
「任意の小文字と繰り返し」、直後に「すべてと0以上の繰り返し」、直後に「任意の小文字と繰り返し」を意味しますか?もしそうなら、なぜ出力は上記のように見えるのですか?
私はできる限りオンラインで調べようとしましたが、それでも理解できませんでした。どんな助けでも大歓迎です。ありがとうございました。