Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regex not matching as expected

by graff (Chancellor)
on Nov 07, 2015 at 03:53 UTC ( [id://1147162]=note: print w/replies, xml ) Need Help??


in reply to Regex not matching as expected

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

Replies are listed 'Best First'.
Re^2: Regex not matching as expected
by AnomalousMonk (Archbishop) on Nov 07, 2015 at 04:45 UTC

    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:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found