Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I seek your wisdom: I write this very often when parsing files line-by-line ("if this line looks like this, it's this kind of data, elsif this line looks like this, it's this other kind of data, etc.") so I was wondering if there is a shorthand for the regex /^\s*abc\s*$/, or "the only thing on this line is the letters abc with maybe some whitespace"? I know I can trim the whitespace off first, but maybe there is a way to get rid of the ^$ too?

Replies are listed 'Best First'.
Re: Shorthand for /^\s*abc\s*$/ ?
by Athanasius (Archbishop) on Oct 11, 2015 at 13:13 UTC

    I like AppleFritter’s approach. But, in the spirit of TMTOWTDI, here’s another, which builds the required regex via a subroutine call:

    #! perl use strict; use warnings; my %searches = ( bar => " bar \n\n", foo => " foo c \n", ); for (sort keys %searches) { print "$_: ", $searches{$_} =~ re($_) ? 'match' : 'no match', "\n" +; } sub re { qr{ ^ \s* $_[0] \s* $ }x; }

    Output:

    23:06 >perl 1402_SoPW.pl bar: match foo: no match 23:06 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Shorthand for /^\s*abc\s*$/ ?
by AppleFritter (Vicar) on Oct 11, 2015 at 13:05 UTC

    If you're doing a lot of these tests for different values of "abc", I'd suggest just trimming the whitespace once and then working with the trimmed string:

    # ... use String::Trim; while(<>) { trim; given($_) { when("abc") { say "Got 'abc'!"; } when("cmc") { say "Got 'cmc'!"; } when("uep") { say "Got 'uep'!"; } } }

    If you're looking to do more complicated processing that nonetheless involves a lot of boilerplate, you could build a list of regular expressions on-the-fly:

    sub got_abc { say "Got 'abc'!"; } sub got_cmc { say "Got 'cmc'!"; } sub got_uep { say "Got 'uep'!"; } sub got_a3 { say "Got 'a{3}'!"; } my %interesting = ( "abc" => \&got_abc, "cmc" => \&got_cmc, "uep" => \&got_uep, "a{3}" => \&got_a3, ); # add boilerplate my %tests = map { qr/^\s*$_\s*$/ => $interesting{$_} } keys %interesting; # process data and apply tests while(<>) { chomp; foreach my $test (keys %tests) { $_ =~ $test and &{ $tests{$test} }; } }

    All

      I like this approach, AppleFritter, but it could perhaps be made slightly simpler (or maybe not simpler but only more concise):
      use strict; use warnings; use 5.014; my %interesting; for (qw/ abc cmc uep a{3}/) { $interesting{$_} = sub { say "got '$_'!"}; } # process data and apply tests for (@ARGV) { foreach my $test (keys %interesting) { /^\s*$test\s*$/ and $interesting{$test}->(); } }
      Running it:
      $ perl regexes.pl abc uep cmc aaa foo bar got 'abc'! got 'uep'! got 'cmc'! got 'aaa'!
Re: Shorthand for /^\s*abc\s*$/ ?
by Anonymous Monk on Oct 11, 2015 at 15:30 UTC

    Thanks very much Athanasius and AppleFritter, dynamically building the regexes was the piece of inspiration I was missing :)