CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Replacing capture data

 



ssdp
New User

Jun 4, 2008, 5:17 AM

Post #1 of 4 (440 views)
Replacing capture data Can't Post

Hi All,
I am new to this forum.
I am trying to replace captured string after matching a pattern.

ex. $string = "Revision_Number=8" # this is a line from sample config file

$string =~ s/^Rev.*=(\d*)/(what i hv to enter) / #here i want to replace the captured data $1 with $1+1 (8 with 9)

Is it possible to replace the captured data like "Revision_Number=9" using only one match/replace statement?

Thanks,


rajiv_chennai
Novice

Jun 4, 2008, 5:29 AM

Post #2 of 4 (438 views)
Re: [ssdp] Replacing capture data [In reply to] Can't Post

Yes We can do the following code will do that job


Code
 #!/usr/bin/perlmy $line="rajiv=19";$line=~ s/rajiv=(\d+)/$1+1/e;print $line;



In perl regexp e modifier is stand for expression.

Regards

Rajiv


ssdp
New User

Jun 4, 2008, 10:02 PM

Post #3 of 4 (409 views)
Re: [rajiv_chennai] Replacing capture data [In reply to] Can't Post

Hi Rajiv,
Good try, but when you do a substitution it will substitute entire line by 20 (not as expected rajiv=20)


rajiv_chennai
Novice

Jun 4, 2008, 10:47 PM

Post #4 of 4 (406 views)
Re: [ssdp] Replacing capture data [In reply to] Can't Post

Try this


Code
#!/usr/bin/perl 

my $line="rajiv=19";

$line=~ s/(rajiv=)(\d+)/$2+1/e;

$line=$1.$line;

print $line;


Regards
Rajiv

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives