kiat has asked for the wisdom of the Perl Monks concerning the following question:
Is there a way to pass a variable that tells the regex to toggle case-sensitivity (with or without the i switch)?
Thanks in advance :)my @words = qw/one One/; my $pattern = 'one'; foreach (@words) { # case-sensitive so 'one' is matched but not 'One' # but I would to to be able to enable case-sensitivity # in some cases to match both 'one' and 'One' if (/$pattern/) { print "Matched: pattern was $pattern and token was $_\n"; } else { print "Unmatched: pattern was $pattern and token was $_\n"; } }
Update: Thanks to dave_the_m and tlm. Got it to work as follows:
my $case = shift; my $pattern = $case ? qr/$pattern/i : qr/$pattern/; if (/$pattern/) { # code }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex help
by tlm (Prior) on Apr 24, 2005 at 13:18 UTC | |
|
Re: Regex help
by dave_the_m (Monsignor) on Apr 24, 2005 at 13:11 UTC | |
|
Re: Regex help
by Crackers2 (Parson) on Apr 24, 2005 at 17:20 UTC | |
by kiat (Vicar) on Apr 25, 2005 at 01:27 UTC | |
|
Re: Regex help
by TedPride (Priest) on Apr 24, 2005 at 23:33 UTC | |
by kiat (Vicar) on Apr 25, 2005 at 01:29 UTC |