Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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
[Eily]: It can also be used to force a copy, and remove magic from a variable
[Eily]: there was a post some time ago about variable coming from regex taking more space than the same string defined directly IIRC
[Eily]: and if there's XS, make sure the scalar holds a string representation, that sounds like bad practice though (forcing that on the call side)
[choroba]: You should never pass $1 without double quotes to a sub
[choroba]: or "I should never", at least
[choroba]: that's not the sub's business
[Eily]: choroba but does $name = $1 solve the issue ?
[Eily]: if so, most subs start with my (VARIABLES) = @_ anyway
[moritz]: for short subs, I sometimes let them work on $_[0] directly

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2018-02-22 17:18 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (296 votes). Check out past polls.