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

Re: Problems with matching and metacharacters

by WebHick (Scribe)
on Jun 14, 2001 at 19:00 UTC ( #88444=note: print w/replies, xml ) Need Help??


in reply to Problems with matching and metacharacters

Simply put \Q and \E around $input. Like so:
 m/(\Q$input\E)/

Don't do this:
m/\Q($input)\E/
Because the regex will also try to match the parenthesis.

Sarah
Update: I never said I was fast.

Replies are listed 'Best First'.
Re: Re: Problems with matching and metacharacters
by Anonymous Monk on Jun 14, 2001 at 19:55 UTC
    Actually, the \E is not needed, since $input is the only thing in the regex; that is, it is automatically ended.
    /(\Q$input)/
    Here's a question: Why would you need to put parentheses around $input? You can access its value more efficiently as $input, than as $1, since parentheses would slow down the regex. More succintly, change the code to this:
    /\Q$input/
    And change occurences of $1 to $input

      Actually, /(\Q$input)/ produces the error:

      /(\(\)/: unmatched () in regexp

      because you're now matching $input), not just $input. If the parenthesis are to stay, you'll need the \E

      Sarah

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2022-10-01 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?