develooper Front page | perl.beginners | Postings from April 2003

Re: help with substiution

Thread Previous | Thread Next
From:
Pete Emerson
Date:
April 28, 2003 10:45
Subject:
Re: help with substiution
Message ID:
Pine.LNX.4.44.0304281336550.30323-100000@emerson.wss.yale.edu
Apr 28, 2003 at 10:29am from Ravi Malghan:

RM> $_ = "attr1 val1\nattr2 val2\nattr3 val3";
RM> 
RM> I want to substitute "attr2 val2" with "attr2 val4"
RM> s/.*(attr2 .*)\nattr3.*/"attr2 val4"/;

s/attr2 val2/attr2 val4/;

If you want to capture the "attr2 val4", or if you don't know what val2 is 
going to be, that's okay too:

s/attr2 (\S+)/attr2 val4/;

and now val2 is stored in $1.

Note that if you want to match your regex across the \n, treat the string 
as a single string by adding an 's' flag to the end of the regex. That 
doesn't apply in this case, since what you're relacing is all on one line.

Pete

-- 
http://emerson.wss.yale.edu/perl
Pete Emerson
WSS AM&T Yale University



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About