For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > qr() and escaping a RegEx string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author qr() and escaping a RegEx string
Yitzle

2007-10-28, 4:00 am

I want to search some text for a user provided string.
I was getting input and escaping it with qr(). I then used the qr()'ed
value as input to my grep.
However, I realized that qr() works too well for my pursposes. I want
the user input to be interpretted as a string literal, not as a RegEx,
ie if the user inputs "[ab]", I want to search for "\[ab\]" - that
exact sequence of characters.
How do I go about searching for a string literal in a bigger block of text?

Thanks!
Gunnar Hjalmarsson

2007-10-28, 4:00 am

yitzle wrote:
> I want to search some text for a user provided string.
> I was getting input and escaping it with qr(). I then used the qr()'ed
> value as input to my grep.
> However, I realized that qr() works too well for my pursposes. I want
> the user input to be interpretted as a string literal, not as a RegEx,
> ie if the user inputs "[ab]", I want to search for "\[ab\]" - that
> exact sequence of characters.
> How do I go about searching for a string literal in a bigger block of text?


Check out the FAQ entry

perldoc -q supplied

"How do I match a pattern that is supplied by the user?".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
John W . Krahn

2007-10-28, 4:00 am

On Saturday 27 October 2007 21:18, yitzle wrote:
> I want to search some text for a user provided string.
> I was getting input and escaping it with qr(). I then used the
> qr()'ed value as input to my grep.
> However, I realized that qr() works too well for my pursposes. I want
> the user input to be interpretted as a string literal, not as a
> RegEx, ie if the user inputs "[ab]", I want to search for "\[ab\]" -
> that exact sequence of characters.
> How do I go about searching for a string literal in a bigger block of
> text?


Either use index() to search for literal text or use quotemeta to
escape regular expression meta-characters.



John
--
use Perl;
program
fulfillment
Yitzle

2007-10-28, 4:00 am

Thanks. That answers my question!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com