in reply to Re^3: regex question - dynamically address captures
in thread regex question - dynamically address captures
my $line = q{Convention_9841_12345678901234.txt}; my @captures; print "\nIncorrect\n\n"; print_captures( incorrect( qr/Convention_(\d{4})_(\d{14})\.txt$/, $lin +e ) ); print_captures( incorrect( qr/Convention_(\d{4})_\d{14}\.txt$/, $line +) ); print_captures( incorrect( qr/Convention_\d{4}_\d{14}\.txt$/, $line ) +); print_captures( incorrect( qr/Convention_((\d{4})_(\d{14}))\.txt$/, $l +ine ) ); print_captures( incorrect( qr/^(.*Convention_(\d{4})_(\d{14})\.txt)$/, + $line ) ); print "\nCorrect\n\n"; print_captures( get_captures( qr/Convention_(\d{4})_(\d{14})\.txt$/, $ +line ) ); print_captures( get_captures( qr/Convention_(\d{4})_\d{14}\.txt$/, $li +ne ) ); print_captures( get_captures( qr/Convention_\d{4}_\d{14}\.txt$/, $line + ) ); print_captures( get_captures( qr/Convention_((\d{4})_(\d{14}))\.txt$/, + $line ) ); print_captures( get_captures( qr/^(.*Convention_(\d{4})_(\d{14})\.txt) +$/, $line ) ); sub incorrect { my ($pattern, $line) = @_; my @captures; (@captures) = $line =~ m/$pattern/ or return; return @captures; } sub get_captures { my ($pattern, $line) = @_; $line =~ m/$pattern/ or return; return map {substr $line, $-[$_], $+[$_] - $-[$_]} (1 .. $#-); } sub print_captures { my @captures = @_; @captures ? print "captures: @captures\n" : print "no captures\n" +; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: regex question - dynamically address captures
by ikegami (Patriarch) on Jul 19, 2006 at 04:57 UTC | |
by jkeenan1 (Deacon) on Jul 20, 2006 at 01:11 UTC |