in reply to Re^2: Empty pattern in regex
in thread Empty pattern in regex
Output:#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; $_ = 'abacad'; say "/a(.)/"; if (/a(.)/g) { say "\$1: $1"; say "\$&: $&"; } else { say 'No match'; } for my $try (1 .. 3) { say "//"; if (//g) { say "\$1: $1"; say "\$&: $&"; } else { say 'No match'; } }
/a(.)/ $1: b $&: ab // $1: c $&: ac // $1: d $&: ad // No match
Update: If I remember correctly, this was the original reason the feature was introduced:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my $x = 'found 11'; my $y = 'found 12'; if ($x =~ /found (\d+)/ && $y =~ //) { # No need to repeat the long r +egex! Yay! say "Found $1."; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Empty pattern in regex
by perlboy_emeritus (Scribe) on Oct 20, 2023 at 20:08 UTC |