Conditional options to a regexp match?

D

David Wake

I want to do a case-insensitive regexp match if and only if the variable
$insensitive is set.

One way would be

if ($insensitive) {
/($pattern)/i;
}
else {
/($pattern)/;
}

Is there any way to do this with only one line of regexp searching?

I tried this:

my $searchoption = "";
$searchoption .= "i" if $insensitive;
/($pattern)/$searchoption;

but I get a syntax error.

Obviously, if we have three or four options the problem gets
exponentially bigger. For example, options for "i", "s" and "g" would
require eight possibilities. So it would be a big help if there is an
easy way to do this!

Thanks,

David
 
M

Michael P. Broida

David said:
I want to do a case-insensitive regexp match if and only if the variable
$insensitive is set.

One way would be

if ($insensitive) {
/($pattern)/i;
}
else {
/($pattern)/;
}

Is there any way to do this with only one line of regexp searching?

I tried this:

my $searchoption = "";
$searchoption .= "i" if $insensitive;
/($pattern)/$searchoption;

but I get a syntax error.

Obviously, if we have three or four options the problem gets
exponentially bigger. For example, options for "i", "s" and "g" would
require eight possibilities. So it would be a big help if there is an
easy way to do this!

Something with "eval"?? Something like:

$matchstatement = "\$x = m/(\$pattern)/"; # add escapes (\) as needed
if ($insensitive) $matchstatement .= "i"; # append desired options
if ($global) $matchstatement .= "g"; # ditto
...
eval $matchstatement; # I don't know the "eval" syntax :)

Don't know if that's "safe", but it seems like it
oughta work. :)

Mike
 
J

John W. Krahn

David said:
I want to do a case-insensitive regexp match if and only if the variable
$insensitive is set.

One way would be

if ($insensitive) {
/($pattern)/i;
}
else {
/($pattern)/;
}

Is there any way to do this with only one line of regexp searching?

I tried this:

my $searchoption = "";
$searchoption .= "i" if $insensitive;
/($pattern)/$searchoption;

but I get a syntax error.

Obviously, if we have three or four options the problem gets
exponentially bigger. For example, options for "i", "s" and "g" would
require eight possibilities. So it would be a big help if there is an
easy way to do this!


/(?@{[ $insensitive ? 'i' : '' ]}:($pattern))/;



John
 
C

Cognition Peon

9:24pm, IP packets from John W. Krahn delivered:
/(?@{[ $insensitive ? 'i' : '' ]}:($pattern))/;

Great solution. Thanks!! Never seen an example of
(?imsx) regular expression extension before.
 
D

David Wake

Michael P. Broida said:
Something with "eval"?? Something like:

$matchstatement = "\$x = m/(\$pattern)/"; # add escapes (\) as needed
if ($insensitive) $matchstatement .= "i"; # append desired options
if ($global) $matchstatement .= "g"; # ditto
...
eval $matchstatement; # I don't know the "eval" syntax :)

Don't know if that's "safe", but it seems like it
oughta work. :)

Mike


"eval" was what I needed. Thanks!

David
 
A

Anno Siegel

Cognition Peon said:
9:24pm, IP packets from John W. Krahn delivered:
/(?@{[ $insensitive ? 'i' : '' ]}:($pattern))/;

Great solution. Thanks!! Never seen an example of
(?imsx) regular expression extension before.

Look for the keyword "cloistered" in perlre for the whole story.

Anno
 
C

Cognition Peon

10:44pm, IP packets from Anno Siegel delivered:
Cognition Peon said:
9:24pm, IP packets from John W. Krahn delivered:
/(?@{[ $insensitive ? 'i' : '' ]}:($pattern))/;

Great solution. Thanks!! Never seen an example of
(?imsx) regular expression extension before.

Look for the keyword "cloistered" in perlre for the whole story.

Now, I see the wonderful Monastery :)
 
J

John W. Krahn

Cognition said:
9:24pm, IP packets from John W. Krahn delivered:
/(?@{[ $insensitive ? 'i' : '' ]}:($pattern))/;

Great solution. Thanks!! Never seen an example of
(?imsx) regular expression extension before.

Actually it is an example of the (?imsx:) extension. Using the (?imsx)
extension it would be:

/(?@{[ $insensitive ? 'i' : '' ]})($pattern)/;


:)
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
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top