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

Hi All,
Can you please help me out to write this regular expression. I am sure this may be simple for you
I want to find whether the string has got a '&' sign in the begining or in the end or not.
If founds, remove this -> &. Here is the code which I am looking ahead
$str = "&ABCD" <Regular expression should remove the & and put the ABCD to string $st +r> or $str = "ABCD&" <same way, Regular expression should remove the & and put the ABCD to +string $str>
so, $str always should contains, just ABCD. Thanks and Best Regards, Dan.

Replies are listed 'Best First'.
Re: Reg. Expression - Replace " from string
by GrandFather (Saint) on Jun 15, 2006 at 21:38 UTC

    Give a fish, or teach him to fish? /me dons tutor's hat.

    Read the Tutorials sections here for an introduction to regexen. In particular take note of the magical characters that match the start (^) and end ($) of a string. Note also the alternation (|) character. Also read the Perl man pages perlretut, perlrequick and perlre.


    DWIM is Perl's answer to Gödel
Re: Reg. Expression - Replace " from string
by albert (Monk) on Jun 15, 2006 at 23:33 UTC
    I second the recommendation to check the documentation. Still to get you started, here are a few things which solve your exact questions.
    # Remove first & $str =~ s/^&//; # Remove last & $str =~ s/&$//; # Remove all & $str =~ s/&//g;
    -a

      and to do both requested operations at once:

      $str =~ s/^&|&$//g;

      but that has just given him the fish and if this is homework then he gets the kudos (possibly) without learning anything.


      DWIM is Perl's answer to Gödel
Re: Reg. Expression - Replace " from string
by Jasper (Chaplain) on Jun 16, 2006 at 09:04 UTC
    Wow, astonishingly similar to your first post on these boards (535528).

    You don't learn anything by not understanding the answers, you know...