Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Regex expression is matching more than once

by tobias_hofer (Friar)
on Apr 22, 2013 at 07:51 UTC ( #1029815=perlquestion: print w/replies, xml ) Need Help??
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;


Any hints are highly welcome!
Best regards! Tobias

Replies are listed 'Best First'.
Re: Regex expression is matching more than once
by Corion (Pope) 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! ;-)

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

    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029815]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2018-06-20 17:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.