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

Re: Print only if pattern matches

by Athanasius (Chancellor)
on Jan 24, 2013 at 07:00 UTC ( #1015089=note: print w/replies, xml ) Need Help??

in reply to Print only if pattern matches

The script can be re-written so:

#! perl use strict; use warnings; while (my $line = <DATA>) { if ($line =~ / ^ \d+ \| (\d+?) \| /x && $1 == 1 && $line =~ s{ ^ (.*\|)* # $1 .*ABC\.pi= ([\d.]+|[\w.]+)* # $2 .*ABC\.id= (\d+|[\w.]+) # $3 .* $ } {$1$2|$3}sx) { print "$1$2|$3\n"; } } __DATA__ ...

While this “works”, it is dubious: the * quantifier in a regex means match zero or more of the preceeding; in the substitution, do you really want to match zero occurrences of (.*\|) or ([\d.]+|[\w.]+)? If not, use the + quantifier meaning one or more.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015089]
[LanX]: # python scoping bug
[shmem]: see my scratchpad. Look for userDict

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2018-03-20 18:05 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (256 votes). Check out past polls.