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

I have a situation where text lines can come in four variations:

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

The issue is handling whether the first two columns are digits or N/A. The regex needs to be able to grab the testname as well (in this case, basic_mem_test). This regex worked great when I thought the N/A N/A case was the only one possible:

.*N\/A\s+N\/A\s+(\w+).*

I revised the regex to accept digit cases and it's not working as expected:

 \s*[a-zA-Z0-9\/]+\s+[a-zA-Z0-9\/]+\s+(\w+).*

What am I doing wrong?

Replies are listed 'Best First'.
Re: Regex not matching as expected
by AnomalousMonk (Archbishop) on Nov 06, 2015 at 20:02 UTC

    Your revised  [a-zA-Z0-9\/]+ fails because it is too broad: it matches 'N/A' and '12345', but it also matches 'N/Afoobar1234', 'xyzzy' and '///////'. stevieb's regex is more specific.


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

Re: Regex not matching as expected
by GotToBTru (Prior) on Nov 06, 2015 at 19:57 UTC

    You need to use the or function in regexes.

    @strings = ('A A C D','A A/B C D','A/B A C D','A/B A/B C D'); foreach $string (@strings) { ($third) = $string =~ m/(?:A|A\/B) (?:A|A/B) (\w+) \w+/; printf "%s - %s\n", $string, $third }
    A A C D - C A A/B C D - C A/B A C D - C A/B A/B C D - C

    I put the two alternatives in a group separated by a |, which is the or function. I put them in a group so that it is clear these two alternatives apply only for the first match in the line. The ?: at the beginning means that group should be used for matching purposes but the value should not be captured. I repeat that group because the same two alternatives may show up as the second match.

    Dum Spiro Spero
Re: Regex not matching as expected
by runrig (Abbot) on Nov 06, 2015 at 19:12 UTC
    Why not just split the line and test each element?
    LINE: while (<>) { my ($one, $two, $test_name, @remaining) = split; for ($one, $two) { next LINE unless $_ eq 'N/A' or /^\d+$/; } next unless $test_name =~ /^\w+$/; # process line }

      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
        and then miss the $_ =~ in the latter part of your or.
        $_ =~ is optional before m//.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        "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.
Re: Regex not matching as expected
by stevieb (Canon) on Nov 06, 2015 at 19:33 UTC

    The following regex checks for an integer followed by a dot then by whitespace as the beginning of the line/string, then uses an 'or' to check for either "N/A" or an integer, each with a following whitespace, twice. It then captures the next following word characters.

    ^\d+\.\s+/(?:N\/A\s+|\d+\s+){2}(\w+)
Re: Regex not matching as expected
by graff (Chancellor) on Nov 07, 2015 at 03:53 UTC
    When you say "not working as expected", it would be helpful to say in particular how the actual result differed from the expected result.

    In your list of four variations, are the initial digits ("1.", "2.", "3.", "4.") part of the data? If so, is there really variation in the data as to whether the initial period is followed by a space? (It would be better to provide four lines of "actual" data enclosed in code tags.)

    I'd go with the suggestion above about using split - and I see no reason for that to be complicated:

    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { my @flds = split; if ( $flds[0].$flds[1] eq 'N/AN/A' ) { print "nana: $flds[2]\n"; } elsif ( $flds[0] eq 'N/A' ) { print "na1: $flds[2]\n"; } elsif ( $flds[1] eq 'N/A' ) { print "na2: $flds[2]\n"; } else { print "no na: $flds[2]\n"; } } __DATA__ 1234 5678 no_nas_here blah blah 1234 N/A should_be_na2 blah blah N/A 5678 must_be_na1 blah blah N/A N/A nana
    (It seems I may have initially posted an incorrect version of this snippet, and updated it to correct the errors.)

      What about a bad 'N/A' with or without digits?

      c:\@Work\Perl\monks>perl -wMstrict -e "for ( '1234 5678 no_nas_here blah blah', '1234 N/A should_be_na2 blah blah', 'N/A 5678 must_be_na1 blah blah', 'N/A N/A nana', 'foo 9999 fake_nas_1 blah', '9999 foo fake_nas_2 blah blah', 'foo foo fake_nas_3 blah blah blah', ) { my @flds = split; if ( $flds[0].$flds[1] eq 'N/AN/A' ) { print \"nana: $flds[2]\n\"; } elsif ( $flds[0] eq 'N/A' ) { print \"na1: $flds[2]\n\"; } elsif ( $flds[1] eq 'N/A' ) { print \"na2: $flds[2]\n\"; } else { print \"no na: $flds[2]\n\"; } } " no na: no_nas_here na2: should_be_na2 na1: must_be_na1 nana: nana no na: fake_nas_1 no na: fake_nas_2 no na: fake_nas_3


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