develooper Front page | perl.beginners | Postings from September 2011

Re: double substitution

Thread Previous | Thread Next
From:
Jim Gibson
Date:
September 19, 2011 18:18
Subject:
Re: double substitution
Message ID:
CA9D37CD.1492D%JimSGibson@gmail.com
On 9/19/11 Mon  Sep 19, 2011  5:56 PM, "Rajeev Prasad" <rp.neuli@yahoo.com>
scribbled:

> $string="alpha number='42'"
> $string=~s/.*\=// ;
> $string=~s/\'//g;
> 
> to get 42 and not '42'
> 
> 
> can these two substitutions be combined?

If you know what you want to extract, then use capturing:

if( $string =~ /'(\d+)'/ ) {
    $number = $1;
}

or shorter (but not better):

($number) = $string =~ /'(\d+)'/;



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