Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Regex help

by kiat (Vicar)
on Apr 24, 2005 at 13:02 UTC ( [id://450937]=perlquestion: print w/replies, xml ) Need Help??

kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Is there a way to pass a variable that tells the regex to toggle case-sensitivity (with or without the i switch)?

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"; } }
Thanks in advance :)

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

    In general I'd use the qr operator:

    my $pattern = qr/one/i;

    But from your code it is unclear to me exactly how you want to do your "toggling" between case sensitive and case insensitive. Depending on what you actually want to do, one solution might be:

    my $sensitive = qr/one/; my $insensitive = qr/one/i; foreach (@words) { my $pattern = some_condition( $_ ) ? $sensitive : $insensitive; if (/$pattern/) { print "Matched: pattern was $pattern and token was $_\n"; } else { print "Unmatched: pattern was $pattern and token was $_\n"; } }

    Regarding the use of qr, the posts by japhy on this recent thread are very instructive.

    the lowliest monk

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

    Just a little side-note: Your definition of case-sensitivity is backwards. The case-sensitive pattern will match 'one' but not 'One', the case insensitive one will match both.

Re: Regex help
by TedPride (Priest) on Apr 24, 2005 at 23:33 UTC
    If you're only going to be doing a small number of searches on a short string, you can use the i flag for everything without significantly slowing your program. If you're going to be doing a large number of searches and/or the string to be searched is very long, you're better off making a copy and upper or lowercasing everything so you don't have to use i at all.

    Either way, I wouldn't come at the problem from this direction.

      I'm doing a quiz thing. I need the answers to be case-sensitive for some questions and case-insensitive for others.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://450937]
Approved by castaway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-23 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found