http://www.perlmonks.org?node_id=1029815

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

Hi monks!

Following is making me crazy, I just want to delete the substring ".xml" from "..\MyXMLFile.xml" by a trim:

my $FileName = "..\\MyXMLFile.xml"; $FileName =~tr/(\.xml)$//d;

But my string is getting to this: "\MyXMLFile" the leading dots are missing. How comes?

When I do it this way - with a search - it works fine..:
my $FileName = "..\\MyXMLFile.xml"; $FileName =~s/(\.xml)$//i;

Why?

Any hints are highly welcome!
Best regards! Tobias

Replies are listed 'Best First'.
Re: Regex expression is matching more than once
by Corion (Patriarch) on Apr 22, 2013 at 07:54 UTC

    tr replaces characters, while s/// replaces substrings.

    Your code in fact removes all characters in the set lmx.()$ from the target string.

    Update: Athanasius pointed out that I missed the dollar sign in what tr replaces. The expression given to tr// does not interpolate variables.

      Oh.. Thanks a lot!
      Next time I will read the perl doc more carefully!
      Promised! ;-)
      Cheers!
      Tobias

Re: Regex expression is matching more than once (regex expressions don't do stuff)
by Anonymous Monk on Apr 22, 2013 at 08:45 UTC

    Here is a regex expression, it doesn't do anything

    my $rex = qr/(.)/;

    Operators do stuff, here is a regex inside an operator, the substitution operator, replace operator, s///

    $captured_and_released =~ s/$rex/$1/;

    And match operator, m//atch operator, m//

    ( $xerox ) = $original =~ m/$rex/;

    The tr///ansliteration operator does not use regex expressions

    Remember the operator name, its the function that does stuff :)

Re: Regex expression is matching more than once
by Rahul6990 (Beadle) on Apr 22, 2013 at 08:47 UTC
    Hi,

    tz/// will treat .xml as four different independent character and tries replace occurrence of each character with nothing, as a result of which your output string does not have dots in it.Which is not the case with s/// as it consider .xml as a single string and replace the combined occurrence with nothing.