以前にも近い話題でひとつ書いたのですが、 note103.hateblo.jp それと重なりながらもちょっとズレるトピックでけっこうハマったので、おもに未来の自分用にまとめておきます。 例題 以下の文字列に対して、最初の apple だけマッチさせるパターンを考えてください。 appleorangeapple という問題があったとき、「最初の apple だけ」ということは、いわゆる「最短マッチ(非欲張り型)」のパターンを考えることになりますから、Perl だとこんな感じで作れます。 Perl my $foo = 'appleorangeapple'; say "最短マッチ: $1" if $foo =~ /^(.*?e)/; say "最長マッチ: $1" if $foo =~ /^(.*e)/; 実行。 最短マッチ: apple 最長マッチ: appleorangeapple パターン