in reply to Regex not matching as expected
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:
(It seems I may have initially posted an incorrect version of this snippet, and updated it to correct the errors.)#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex not matching as expected
by AnomalousMonk (Archbishop) on Nov 07, 2015 at 04:45 UTC |