techtween has asked for the wisdom of the Perl Monks concerning the following question:

hi i am new to perl, i have a doubt, $var1=ab_domino_1.xml i need the result when $var1 is printed as, ab_domino_thumb_1.jpg could you suggest regular expression for it

Replies are listed 'Best First'.
Re: need regular expression
by jethro (Monsignor) on Jul 13, 2011 at 12:19 UTC
    $var1=~s/\.xml$/.jpg/;

    This would substitute .xml at the end of a string with .jpg. '$' matches at end of string, '.' has to be escaped with '\' since it would have special meaning otherwise.

      thanks jethro, but still its unfinished coz, $var1= ab_db_1.xml when print $var1 is given i need result as, ab_db_thumb_1.jpg kindly note the difference.

        You have been given the approach that you would use. See perlre for more information. To get more assistance, please show what you have tried. We try to teach you how to do your work -- we do not do your work for you.

        --MidLifeXis

Re: need regular expression
by Anonymous Monk on Jul 13, 2011 at 12:19 UTC

    hi i am new to perl, i have a doubt

    Read perlintro#Simple substitution

    s/foo/bar/; # replaces foo with bar in $_ $a =~ s/foo/bar/; # replaces foo with bar in $a $a =~ s/foo/bar/g; # replaces ALL INSTANCES of foo with bar in $a
    Combine with anchor
    $ end of string
    perlintro also explains the safety net and string literals