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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: regexp w/ special chars by davido
in thread regexp w/ special chars by LanceDeeply

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2021-06-19 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (92 votes). Check out past polls.

    Notices?