Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

regexp w/ special chars

by LanceDeeply (Chaplain)
on Aug 27, 2003 at 19:50 UTC ( [id://287146]=perlquestion: print w/replies, xml ) Need Help??

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

how can I substitute $token with $date in $string?
my $token = '[DATE:%Y-%m-%d]'; my $date = '2003-08-27'; my $string = '/dropArea/dropClient/file_[DATE:%Y-%m-%d].txt';

Replies are listed 'Best First'.
Re: regexp w/ special chars
by tedrek (Pilgrim) on Aug 27, 2003 at 19:56 UTC
    $string =~ s/\Q$token\E/$date/;

    \Q quotes metacharacters in a regex and \E ends the quoting. See perlre

      nice!

      thanks for link, i'm an re noob.

Re: regexp w/ special chars
by shenme (Priest) on Aug 27, 2003 at 19:59 UTC
    Use \Q and \E to 'quote' the string being interpolated into the search:
    $string =~ s/\Q$token\E/$date/;
    Otherwise you get a nasty message:
    Invalid [] range "Y-%" in regex; marked by <-- HERE in m/[DATE:%Y-% <-- HERE m-%d]/

    Or you could use quotemeta() on the string beforehand?

Re: regexp w/ special chars
by davido (Cardinal) on Aug 28, 2003 at 16:56 UTC
    Perl gives you several mechanisms to ensure that you are able to put what might otherwise look like metacharacters into a match string as literal characters rather than metacharacters. In your example above, the cleanest method would be to write the substitution regexp like this:

    $string =~ s/\Q$token\E/$date/;

    In the preceeding example, the \Q and \E tell the regexp engine to treat everything between those two tags as literal characters. They can also be used within the text of a regexp, for example:

    $test_string =~ s/\Q[A-Z]{3}\E([A-Z]{3})/$1/;
    In the preceeding example, everything between the \Q and \E is treated as literal characters, and everything else is treated as a regular expression (which matches any letter between A and Z repeated three times).

    Another way to do it is to employ the quotemeta() function like this:

    my $token = quotemeta ( '[DATE:%Y-%m-%d]' );

    And then write the substitution regexp like this:

    $string =~ s/$token/$date/;

    You'll have to decide which of those methods makes the most sense for your particular situation. There is YAWTDI (Yet Another Way To Do It). That is to escape the characters that would be seen as special (or meta-)characters.

    my $token = '\[DATE:%Y-%m-%d\]';

    And then you would write the substitution regexp like this:

    $string =~ s/$token/$date/;

    The third method is sometimes clumsy, and sometimes efficient. If, for example, the string contained in $token was intended to actually contain some metacharacters, and at the same time, contain literal characters that would be mistaken as metacharacters, escaping the literals that could be mistaken as metas suddenly becomes a better way to do it.

    Hope this helps.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-20 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found