[tpm] Regex question

Indy Singh indy at indigostar.com
Tue Nov 6 11:53:54 PST 2007


> my version (untested) is this:
> s/^foo.*$//mg;

The problem is that this leaves a \n behind on each line.  I want to
remove the complete line.  So here is my latest attempt, which seems to
pass all the cases that I care about including one at the end without a
terminating new line.

$x =~ s/^foo.*?(\n|$)//mg;


# TEST SCRIPT
$x='foo one
foo two
other foo three
foo again
more stuff
foo last';
print "x=$x\n";
$x =~ s/^foo.*?(\n|$)//mg;
print "x=$x\n";




Indy Singh
IndigoSTAR Software -- www.indigostar.com


----- Original Message ----- 
From: "Uri Guttman" <uri at stemsystems.com>
To: "Emil Janev" <ejanev at gmail.com>
Cc: <tpm at to.pm.org>
Sent: Tuesday, November 06, 2007 2:30 PM
Subject: Re: [tpm] Regex question


>>>>>> "EJ" == Emil Janev <ejanev at gmail.com> writes:
>
>  EJ> Hi Indy,
>  EJ> Instead of:
>
>  EJ> while ($x =~ s//(^|$)foo.*?\n//gs) {1;}
>
>  EJ> I tried:
>
>  EJ> $x =~ s/(^|$)foo.*?\n//mgs;
>
> the /s is bad there as it makes . match newline so that could run over
> a
> line. even if it works, it is misleading as you don't want . to match
> newline.
>
> i don't know what the (^|$) is for. $ at the beginning of a regex
> makes
> no sense and ^ will always match as it has /m set and there are
> lines. so the |$ should never even be hit.
>
> my version (untested) is this:
>
> s/^foo.*$//mg;
>
> uri
>
> -- 
> Uri Guttman  ------  uri at stemsystems.com  --------
> http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and
> Coding-
> Search or Offer Perl Jobs  ----------------------------
> http://jobs.perl.org
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm



More information about the toronto-pm mailing list