undo the (?i)

J

joeri

Hi all,

I have an RE where I use the (?i) somewhere, but I want to undo that effect
further on in the RE.
Here's an example:

$var="(?i)(forty|fifty)";
$input = "Forty-ninth ST";
if ($input =~ /$var(\-ninth)\s[S|s]t/) {
print "$&\n";
}

Because the $var contains the (?i), it matches the rest of the RE
case-insensitively, but I want
to match everything after the $var case-sensitively. Can I escape the ($i)
or set the scope of it?

Thanks,

J
 
T

Tassilo v. Parseval

Also sprach joeri:
I have an RE where I use the (?i) somewhere, but I want to undo that effect
further on in the RE.
Here's an example:

$var="(?i)(forty|fifty)";
$input = "Forty-ninth ST";
if ($input =~ /$var(\-ninth)\s[S|s]t/) {
print "$&\n";
}

Because the $var contains the (?i), it matches the rest of the RE
case-insensitively, but I want
to match everything after the $var case-sensitively. Can I escape the ($i)
or set the scope of it?

You can again turn it off at some point:

$input =~ /$var(?-i)(-ninth)\s[S|s]t/;

So, -i will prevent case-insensitive matching for the patterns right of
it.

Tassilo
 
T

Tassilo v. Parseval

Also sprach joeri:
Tassilo said:
I have an RE where I use the (?i) somewhere, but I want to undo
that effect further on in the RE.
Here's an example:

$var="(?i)(forty|fifty)";
$input = "Forty-ninth ST";
if ($input =~ /$var(\-ninth)\s[S|s]t/) {
print "$&\n";
}

Because the $var contains the (?i), it matches the rest of the RE
case-insensitively, but I want to match everything after the $var
case-sensitively. Can I escape the ($i) or set the scope of it?

You can again turn it off at some point:

$input =~ /$var(?-i)(-ninth)\s[S|s]t/;

So, -i will prevent case-insensitive matching for the patterns right of
it.
Thanks! I couldn't find it in the docs or google.

You'll find it in perlre.pod under "Extended patterns":

"(?imsx-imsx)"
One or more embedded pattern-match modifiers, to be turned on
(or turned off, if preceded by "-") for the remainder of the
pattern or the remainder of the enclosing pattern group (if
any). [...]

It can indeed be easily overlooked.

Tassilo
 
J

joeri

Tassilo said:
Also sprach joeri:
Tassilo said:
I have an RE where I use the (?i) somewhere, but I want to undo
that effect further on in the RE.
Here's an example:

$var="(?i)(forty|fifty)";
$input = "Forty-ninth ST";
if ($input =~ /$var(\-ninth)\s[S|s]t/) {
print "$&\n";
}

Because the $var contains the (?i), it matches the rest of the RE
case-insensitively, but I want to match everything after the $var
case-sensitively. Can I escape the ($i) or set the scope of it?

You can again turn it off at some point:

$input =~ /$var(?-i)(-ninth)\s[S|s]t/;

So, -i will prevent case-insensitive matching for the patterns right of
it.
Thanks! I couldn't find it in the docs or google.

You'll find it in perlre.pod under "Extended patterns":

"(?imsx-imsx)"
One or more embedded pattern-match modifiers, to be turned on
(or turned off, if preceded by "-") for the remainder of the
pattern or the remainder of the enclosing pattern group (if
any). [...]

It can indeed be easily overlooked.

Here's what my 5.6 docs say. It is indeed there, but I overlooked it...

(?imsx-imsx)
One or more embedded pattern-match modifiers. This is particularly useful
for dynamic patterns, such as those read in from a configuration file, read
in as an argument, are specified in a table somewhere, etc. Consider the
case that some of which want to be case sensitive and some do not. The case
insensitive ones need to include merely (?i) at the front of the pattern.
For example:
$pattern = "foobar";
if ( /$pattern/i ) { } # more flexible: $pattern = "(?i)foobar";
if ( /$pattern/ ) { }Letters after a - turn those modifiers off.

J
 
B

Brian McCauley

joeri said:
$var="(?i)(forty|fifty)";
$input = "Forty-ninth ST";
if ($input =~ /$var(\-ninth)\s[S|s]t/) {
print "$&\n";
}
Can I escape the ($i) or set the scope of it?

/(?:$var)(\-ninth)\s[S|s]t/


--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
J

John W. Krahn

joeri said:
I have an RE where I use the (?i) somewhere, but I want to undo that effect
further on in the RE.
Here's an example:

$var="(?i)(forty|fifty)";

Change that to:

$var = '(?i:forty|fifty)';

Which will limit the effects of case insensitivity to the enclosing
parenthesis.

$input = "Forty-ninth ST";
if ($input =~ /$var(\-ninth)\s[S|s]t/) {
^^^^^
Your character class is trying to match a 'S' or a '|' or a 's'. Do you
really want to match a vertical bar here? The hyphen is not special in
regular expressions, there is no need to backslash it.

print "$&\n";
}


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
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top