For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2004 > dynamic regex









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 dynamic regex
Machoq

2004-10-27, 3:57 pm

I need to take as variable 2 things
a) The pattern to match
b) Match settings like global, case insensitivity etc..

so i want to have a program like

$str = "Japh" ;
$pat = "JA" ;
$key = "gi" ;#global match, case insensitive
print "Voila!" if ($str =~ /$pat/$key);

this errors with Scalar found where operator expected at test.pl
Any alternatives ?
John W. Krahn

2004-10-27, 3:57 pm

Machoq wrote:
> I need to take as variable 2 things
> a) The pattern to match
> b) Match settings like global, case insensitivity etc..
>
> so i want to have a program like
>
> $str = "Japh" ;
> $pat = "JA" ;
> $key = "gi" ;#global match, case insensitive
> print "Voila!" if ($str =~ /$pat/$key);
>
> this errors with Scalar found where operator expected at test.pl
> Any alternatives ?


Modifiers like i can be included in the pattern itself:

my $key = 'i';
my $pat = "(?$key:JA)";


However using modifiers like g would require you to use eval():

print 'Voila!' if eval "$str =~ /$pat/$key";



John
--
use Perl;
program
fulfillment
Sponsored Links







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

Copyright 2008 codecomments.com