I use this script for practicing regex:

#!/usr/bin/env perl use strict; use warnings; use 5.014; my $string; my $pattern; while ( ( say "Input template string:" ) && ( $string = <> ) ) { while ( ( say "Input pattern:" ) && ( $pattern = <STDIN> ) ) { chomp $pattern; my $regex = qr/$pattern/p; $string =~ $regex; say "${^PREMATCH} \x{25a0}\x{25a0}${^MATCH}\x{25a0}\x{25a0} ${ +^POSTMATCH}"; } }

Replies are listed 'Best First'.
Re: Regex practice script
by jwkrahn (Abbot) on Jul 25, 2011 at 08:23 UTC
    while ((say "Input template string:") && ($string = <>) ) { while ((say "Input pattern:") && ($pattern = <STDIN>)) {

    That should be:

    while ((say "Input template string:") && defined(my $string = <>) ) { while ((say "Input pattern:") && defined(my $pattern = <STDIN>)) {

    If you enter "0" for either $string or $pattern your loop will end prematurely.

      If you enter "0" for either $string or $pattern your loop will end prematurely.
      Rubbish.

      It's impossible to enter a false string. All strings entered contain a newline.

      Tested it, not really an issue here, but thanks for paying attention.