Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Want metachars escaped so they are not interpreted in regexp

Reply
Thread Tools

Want metachars escaped so they are not interpreted in regexp

 
 
Markus Dehmann
Guest
Posts: n/a
 
      01-05-2006
I have a pattern that I want to match against some text. The pattern may
contain some chars that happen to be metachars, but I don't want them to
be interpreted as metachars:

my $pattern = 'Hello...?';
$_ = 'Oh Hello x';
if(m/$pattern/){
print "Oh no, it matches!\n"; # it matches indeed!
}

How do I prepare the $pattern properly before using it in the regexp?

I used something like this:

sub encode {
my $s = $_[0];
$s =~ s/(.)/sprintf "\\x%x",ord($1)/ge;
return $s;
}

to encode the pattern before using it in the regexp, but I think it's
inefficient and inelegant...?

Does anyone have a better escape function?

Thanks!
Markus

P.S.: I'm sorry if this is a FAQ (it should be!), but I googled and
didn't find anything.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      01-05-2006
Markus Dehmann <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I have a pattern that I want to match against some text. The pattern may
> contain some chars that happen to be metachars, but I don't want them to
> be interpreted as metachars:


perldoc -f quotemeta

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
robic0
Guest
Posts: n/a
 
      01-06-2006
On Thu, 05 Jan 2006 01:13:48 -0500, Markus Dehmann <(E-Mail Removed)> wrote:

>I have a pattern that I want to match against some text. The pattern may
>contain some chars that happen to be metachars, but I don't want them to
>be interpreted as metachars:
>
>my $pattern = 'Hello...?';
>$_ = 'Oh Hello x';
>if(m/$pattern/){
> print "Oh no, it matches!\n"; # it matches indeed!
>}
>
>How do I prepare the $pattern properly before using it in the regexp?
>
>I used something like this:
>
>sub encode {
> my $s = $_[0];
> $s =~ s/(.)/sprintf "\\x%x",ord($1)/ge;
> return $s;
>}
>
>to encode the pattern before using it in the regexp, but I think it's
>inefficient and inelegant...?
>
>Does anyone have a better escape function?
>
>Thanks!
>Markus
>
>P.S.: I'm sorry if this is a FAQ (it should be!), but I googled and
>didn't find anything.


Be carefull how you use strings mixed with escape characters when they
are called out as declared constants in your program. For the most part
you don't want to fight the expression parser of your interpreter.
After digesting your constant string, I call it "in-solution" like in
Chemistry. When "in-solution" it is safe from mangling of editors and
parsers. Typically, the string you want to match against would be data
read in by your program from a file. In that case it is always "in-solution".
-robic0-


use strict;
use warnings;

my ($pat_convert);

$pat_convert = convertPatternMeta ( 'Hello...?' );
showMatchResult ($pat_convert, 'Hello...? this is a big string x');
showMatchResult ($pat_convert, 'Oh Hello x');

$pat_convert = convertPatternMeta ( '*?+' );
showMatchResult ($pat_convert, 'Hello...? this (*?+) is a big string x');
showMatchResult ($pat_convert, '*?+ and so is this');

## ------------------------------------
## Helpers
##
sub convertPatternMeta
{
my ($pattern) = shift;
my @regx_esc_codes =
(
"\\", '/', '(', ')', '[', ']', '?', '|',
'+', '.', '*', '$', '^', '{', '}', '@'
);
foreach my $tc (@regx_esc_codes) {
# code template for regex
my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
eval $xxx;
if ($@) {
# the compiler will show the escape char, add
# it char to @regx_esc_codes
$@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;
die "$@";
}
}
return $pattern;
}
##
sub showMatchResult
{
my ($pattern, $string) = @_;
my $result_txt = '';
my ($result) = $string =~ /$pattern/;
if ($result) { $result_txt = 'DOES match'}
else { $result_txt = 'Does NOT match' }
print "\nString: $string\n$result_txt\nPattern: $pattern\n";
}
__DATA__

String: Hello...? this is a big string x
DOES match
Pattern: Hello\.\.\.\?

String: Oh Hello x
Does NOT match
Pattern: Hello\.\.\.\?

String: Hello...? this (*?+) is a big string x
DOES match
Pattern: \*\?\+

String: *?+ and so is this
DOES match
Pattern: \*\?\+

 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      01-06-2006
On 5 Jan 2006 08:27:58 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote:

>Markus Dehmann <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> I have a pattern that I want to match against some text. The pattern may
>> contain some chars that happen to be metachars, but I don't want them to
>> be interpreted as metachars:

>
>perldoc -f quotemeta
>
>Anno


Your an absurd example of a lump of lard
-robic0-
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
"Because of 9-11";cops think they can do anything they want to photographers in NY qtraindash7@optonline.net Digital Photography 81 05-27-2005 10:16 PM
Compiled, Not Interpreted! Arpan ASP .Net 1 11-25-2004 03:29 PM
trouble with regex with escaped metachars (URGENT please O:-) Fernando Rodriguez Python 5 11-20-2003 05:48 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments