Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Regex not matching as expected

by runrig (Abbot)
on Nov 09, 2015 at 17:24 UTC ( [id://1147266]=note: print w/replies, xml ) Need Help??


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

"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.

Replies are listed 'Best First'.
Re^4: Regex not matching as expected
by AnomalousMonk (Archbishop) on Nov 09, 2015 at 17:56 UTC
    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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found