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

Match upper and lower case

by benlaw (Scribe)
on Jan 28, 2005 at 23:23 UTC ( [id://426180]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

Hi all,
I am using regular expression to match some words which may lower or upper case, such as "Error", "ERROR", "error", "ErroR".. . I just looking for search any "error" in the message , is there any method?
thx a lot

Replies are listed 'Best First'.
Re: Match upper and lower case
by Zaxo (Archbishop) on Jan 28, 2005 at 23:29 UTC

    Use the /i flag for case-insensitive match.

    if (/error/i) { #. . . }
    or
    my $erre = qr/error/i; $str =~ $erre snd # . . .

    After Compline,
    Zaxo

Re: Match upper and lower case
by jimrobertsiii (Scribe) on Jan 28, 2005 at 23:56 UTC
    IMHO TMTOWTDI
    if (uc $error eq "ERROR"){ # . . . }
    or
    if (lc $error eq "error"){ # . . . }
    Although, I would prefer Zaxo's first example because it seems more efficient (if for no other reason, but conservation of code :-)

    -Jim

      IMHO TMTOWTDI
      if (uc $error eq "ERROR"){ # . . . }
      or
      if (lc $error eq "error"){ # . . . }
      Although, I would prefer Zaxo's first example because it seems more efficient (if for no other reason, but conservation of code :-)
      Note that this is not strictly equivalent to Zaxo's code since /error/i checks (case insensitively) for the presence of the string 'error' and not for equality to it (up to case insentiveness).

      More importantly: "more efficient"? Well, I premise that I'd probably use /error/i or /^error$/i (this really depends on what can there be in $_) myself in most reasonable cases, but running

      #!/usr/bin/perl use strict; use warnings; use Benchmark qw/:all :hireswallclock/; my $test_str='error'; my @test=map { my @chr=("\0", " "); local $_=$test_str; s/./$& ^ $chr[rand 2]/ge} 1..100; no warnings 'void'; cmpthese -30, { regex => sub { /^\Q$test_str\E$/i for @test }, lc => sub { $test_str eq lc for @test} }, 'all'; __END__
      I get
      Benchmark: running lc, regex for at least 30 CPU seconds... lc: 34.068 wallclock secs (34.10 usr 0.00 sys + 0.00 cusr 0 +.00 csys = 34.10 CPU) @ 19917.24/s (n=679178) regex: 31.518 wallclock secs (31.53 usr 0.00 sys + 0.00 cusr 0 +.00 csys = 31.53 CPU) @ 13689.50/s (n=431630) Rate regex lc regex 13690/s -- -31% lc 19917/s 45% --
      and
      Benchmark: running lc, regex for at least 30 CPU seconds... lc: 31.4462 wallclock secs (31.45 usr 0.00 sys + 0.00 cusr +0.00 csys = 31.45 CPU) @ 26138.57/s (n=822058) regex: 31.8579 wallclock secs (31.86 usr 0.00 sys + 0.00 cusr +0.00 csys = 31.86 CPU) @ 14326.49/s (n=456442) Rate regex lc regex 14326/s -- -45% lc 26139/s 82% --
      under Windows 98 and Linux (kernel 2.6.10) respectively. Perl 5.8.6 in both cases...
Re: Match upper and lower case
by gopalr (Priest) on Jan 28, 2005 at 23:59 UTC

    Hi,

    use 'i' for ignore case.

    if ($_=~m#error#i) { .. ## It'll match whole 'error' }
Re: Match upper and lower case
by jbrugger (Parson) on Jan 29, 2005 at 14:54 UTC
    I downvoted you here, i strongly believe that these kind of questions can be found if a little effort would be put in searching older nodes, google, perldoc, books etc. (and so you learn more too!)
    This is imho not a question belonging here anymore (and cirtenly not at The Monastery Gates), thinking, reading and searching should be done before asking, this is lazy.
      Jbrugger, I agree, but maybe the better question is why this question was approved and front-paged ?? node_id=17688 explains the process . Not the first loose approval lately, I think .
        Nothing wrong with approving this node that I can see. If we didn't approve questions that could be self-answered by 30 or less minutes of searching/reading, we wouldn't have very many approved questions.
      It's at least possible that someone might have looked for documentation but because they were using the wrong keywords not have found anything useful. However, I too downvoted this node because the poster didn't even say that he had looked, let alone where he had looked.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://426180]
Approved by Tanktalus
Front-paged by grinder
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.