[tpm] Regex question

Mark Fowle mfowle at navicominc.com
Tue Nov 6 11:14:56 PST 2007

Here is my next attempt:

$x =~ s/(\n|^|\G)foo[^\n]*(\n|$)/\n/g;
The \G is "where I last matched"
Noting this leaves blank lines which I doubt you want.

-----Original Message-----
From: Indy Singh [mailto:indy at indigostar.com] 
Sent: Tuesday, November 06, 2007 2:00 PM
To: Mark Fowle; tpm at to.pm.org
Subject: Re: [tpm] Regex question

That didn't quite work.  The first line is left behind.  Here is the 
result ('foo one' should not be there):

x=foo one
other foo three
more stuff

Indy Singh
IndigoSTAR Software -- www.indigostar.com

----- Original Message ----- 
From: "Mark Fowle" <mfowle at navicominc.com>
To: "Indy Singh" <indy at indigostar.com>; <tpm at to.pm.org>
Sent: Tuesday, November 06, 2007 1:52 PM
Subject: RE: [tpm] Regex question

I think your regex would fail - has four slashes.
I think your solution would fail with foos selarated by another line as
it'll never be at the front.
I changed the expression to \nfoo.*?\n replacing one of the \n
It seems to work as expected.
I'm always scared of regexes being greedy so would probably write


$x='foo one
foo two
other foo three
foo again
more stuff
print "x=$x";
#while ($x =~ s/(^|$)foo.*?\n//gs) {1;}
$x =~ s/\nfoo.*?\n/\n/g;
print "x=$x";

-----Original Message-----
From: toronto-pm-bounces+mfowle=navicominc.com at pm.org
[mailto:toronto-pm-bounces+mfowle=navicominc.com at pm.org] On Behalf Of
Indy Singh
Sent: Tuesday, November 06, 2007 1:40 PM
To: tpm at to.pm.org
Subject: [tpm] Regex question

Hello all,

I have some multiline data as shown below.  I want to remove all
occurances of 'foo.*?' that are at the beginning of a line.  How can I
do that witha regex.  I can do it with a while loop, but is a there a
more elegent way with a single regex?  Perhaps using some look ahead or
look behind assertion.

$x='foo one
foo two
other foo three
more stuff
print "x=$x";
while ($x =~ s//(^|$)foo.*?\n//gs) {1;}
print "x=$x";

toronto-pm mailing list
toronto-pm at pm.org

More information about the toronto-pm mailing list