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

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top