in reply to Re: Regex not matching as expected
in thread Regex not matching as expected

Although using this approach (split) is a logical one, I'd do anything to avoid the use of labels, and if I can do it in a single line of legible code, I'd prefer that over many. Also, you neglect to catch the 1. at the beginning, and then miss the $_ =~ in the latter part of your or.

I did a short benchmark on the two (split vs. my regex) after fixing the errors, just to see for myself for curiosity's sake, and the one liner regex is about twice as fast. (I didn't dig deeper to see where the hangups were, but I suspect they revolve in the numerous regex checks instead of one).

use warnings; use strict; use Benchmark; open my $fh, '<', 'in.txt' or die $!; timethese(1000000, { regex => '_regex', split => '_split', }); sub _regex { seek $fh, 0, 0; while (<$fh>){ if (/^\d+\.\s+(?:N\/A\s+|\d+\s+){2}(\w+)/){ #print "$1\n"; } } } sub _split { seek $fh, 0, 0; LINE: while (<$fh>){ my ($x, $one, $two, $test_name, @remaining) = split; for ($one, $two){ next LINE unless $_ eq 'N/A' or $_ =~ /^\d+$/; #print "$test_name\n"; } } } __END__ Benchmark: timing 1000000 iterations of regex, split... regex: 20 wallclock secs (17.97 usr + 3.04 sys = 21.01 CPU) @ 47 +596.38/s (n=1000000) split: 39 wallclock secs (35.57 usr + 3.35 sys = 38.92 CPU) @ 25 +693.73/s (n=1000000)

Contents of in.txt:

1. N/A 17497118 basic_mem_test 17036us FAIL 1 2. 17497118 N/A basic_mem_test 17036us FAIL 1 3. 17497118 17497118 basic_mem_test 17036us FAIL 1 4. N/A N/A basic_mem_test 17036us FAIL 1

Replies are listed 'Best First'.
Re^3: Regex not matching as expected
by choroba (Cardinal) on Nov 08, 2015 at 21:11 UTC
    and then miss the $_ =~ in the latter part of your or.
    $_ =~ is optional before m//.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Regex not matching as expected
by runrig (Abbot) on Nov 09, 2015 at 17:24 UTC
    "1" at the beginning? Whatever. I assumed the OP was just enumerating the possibilities, not making that part of the data (and IMHO I still tend to think that). Also, IMO the one-line regex is not very "readable" no matter how you do it.
      I assumed the OP was just enumerating the possibilities ...

      That's a good guess, and it's my guess too, but it's only a guess. Without reading too much between the lines (or the data records), I suspect it may have been stevieb's intention gently to make a point about ambiguity in the OP. (graff makes this point explicitly.)

      ... the one-line regex is not very "readable" no matter how you do it.

      How about if you do it in a readable manner? I like the approach of factoring out regex sub-pattern elements. While more verbose, this eases maintenance, supports use of common elements for other data validation and extraction purposes, and is easily extended.

      c:\@Work\Perl\monks>perl -wMstrict -le "my $na = qr{ N/A }xms; my $n = qr{ \d+ }xms; my $n_or_na = qr{ (?: $na | $n) \s+ }xms; my $test_name = qr{ [[:alpha:]] [_[:alpha:]]* }xms; ;; for my $str ( 'N/A 17497118 basic_mem_test 17036us FAIL 1', '17497118 N/A basic_mem_test 17036us FAIL 1', '17497118 17497118 basic_mem_test 17036us FAIL 1', 'N/A N/A basic_mem_test 17036us FAIL 1', 'foo 12345678 fake_mem_test 999us FAIL 1', '12345678 foo fake_mem_test 999us FAIL 1', 'foo foo fake_mem_test 999us FAIL 1', ) { print qq{'$str'}; if ($str =~ m{ \A \s* $n_or_na{2} ($test_name) }xms) { print qq{ match, grabbed '$1'}; } else { print ' NO match'; } } " 'N/A 17497118 basic_mem_test 17036us FAIL 1' match, grabbed 'basic_mem_test' '17497118 N/A basic_mem_test 17036us FAIL 1' match, grabbed 'basic_mem_test' '17497118 17497118 basic_mem_test 17036us FAIL 1' match, grabbed 'basic_mem_test' 'N/A N/A basic_mem_test 17036us FAIL 1' match, grabbed 'basic_mem_test' 'foo 12345678 fake_mem_test 999us FAIL 1' NO match '12345678 foo fake_mem_test 999us FAIL 1' NO match 'foo foo fake_mem_test 999us FAIL 1' NO match


      Give a man a fish:  <%-{-{-{-<