dynamic regex

M

Machoq

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 ?
 
J

John W. Krahn

Machoq said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,432
Latest member
GTRNorbert

Latest Threads

Top