Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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;

Why?

Any hints are highly welcome!
Best regards! Tobias

Comment on Regex expression is matching more than once
Select or Download Code
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! ;-)
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029815]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2015-07-28 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls