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

I'm using index($mystr,"a") but I want to be able to check for "A" as well as "a".

Is it possible to make index case insensitive?

Originally posted as a Categorized Question.

  • Comment on How can I find the first position of a substring case-insensitively?

Replies are listed 'Best First'.
Re: How can I find the first position of a substring case-insensitively?
by eg (Friar) on Dec 02, 2000 at 11:55 UTC
Re: How can I find the first position of a substring case-insensitively?
by ambrus (Abbot) on Oct 01, 2003 at 12:43 UTC

    You could use regexps, like this:

    $needle = "a"; $haystack =~ /\Q$needle\E/i; $index = $-[0];
    this works even if $needle has regexp-metacharacters in it.

    But @- exists only in newer perls (from 5.6.1 I think). In perl 5.005 you could use something like

    $needle = "a"; $haystack =~ /\Q$needle\E/i; $index = pos($haystack)-length($needle);
    or
    $needle = "a"; $haystack =~ /\Q$needle\E/i; $index = length($`);
    but note that $` makes perl slower.

    Whitespace added by tye

Re: How can I find the first position of a string within a string
by zejames (Hermit) on Dec 01, 2000 at 18:25 UTC
    I think you could simply do this :

    my $position = index(lc($mystr),"a");

    You first "lowercase" the string, and then look for your substring into it.

    HTH
    James
Re: How can I find the first position of a string within a string
by chipmunk (Parson) on Dec 01, 2000 at 19:46 UTC
    How about a regex?
    if ($mystr =~ /(?=a)/gi) { $position = pos($mystr); }
    I use a positive lookahead assertion so that pos() will correspond to the start of the match, rather than the end.
Re: How can I find the first position of a substring case-insensitively?
by ambiguous (Novice) on Dec 03, 2000 at 16:54 UTC
    As far as I know you cannot make index() case insensitive.
    I think the simpliest thing to do is to make you string case insensitive.
    Ex.
    my $lcstr = lc $mystr; my $ind = index($lcstr,"a");
    This way "a" matches the first instance of "A" or "a" in the original $mystr.
    Hope that helps.

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by Anonymous Monk on Dec 05, 2000 at 09:01 UTC
    index(lc $mystr,"a")

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by tektsu (Acolyte) on Dec 06, 2000 at 23:44 UTC
    How about index(lc $mystr,"a")?<br
    I'm not sure how efficient it is, but it does work...

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by tenatious (Beadle) on Dec 05, 2000 at 23:19 UTC

      It's kind of a pain in the butt, but here's one way:

      my $haystack = "foobar"; my $needle = "Bar"; $haystack =~ /($needle)/gi; print ((pos $haystack) - (length $needle));
Re: How can I find the first position of a substring case-insensitively?
by kilinrax (Deacon) on Dec 01, 2000 at 21:20 UTC
    You could just use 'lc' to test for 'a' in the lowercased version of the string (which would effectively be the same thing).
    index(lc($mystr),'a')

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by ariels (Curate) on Dec 03, 2000 at 14:35 UTC
    index(lc($mystr), "a");.

    If you want to look for $mysubstr instead of for "a", and you don't know that it's all lowercase, you'll need to apply lc to that as well, of course.

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by ariels (Curate) on Dec 03, 2000 at 14:35 UTC
    index(lc($mystr), "a");.

    If you want to look for $mysubstr instead of for "a", and you don't know that it's all lowercase, you'll need to apply lc to that as well, of course.

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by Anonymous Monk on Dec 10, 2000 at 03:29 UTC
    index(lc $mystr,"a")

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by ambrus (Abbot) on Oct 01, 2003 at 12:55 UTC
    You could use regexps, like this: $needle="a"; $haystack=~/\Q$needle/i; $index=$-[0]; this works even if $needle has regexp-metacharacters in it. But @- exists only in newer perls (from 5.6.1 I think). In perl 5.005 you could use something like $needle="a"; $haystack=~/\Q$needle/i; $index= pos($haystack)-length($needle) or $needle="a"; $haystack=~/\Q$needle/i; $index= length($`); but note that $` makes perl slower.

    Originally posted as a Categorized Answer.

Re: How can I find the first position of a substring case-insensitively?
by ambrus (Abbot) on Oct 01, 2003 at 12:56 UTC
    You could use regexps, like this: $needle="a"; $haystack=~/\Q$needle/i; $index=$-[0]; this works even if $needle has regexp-metacharacters in it. But @- exists only in newer perls (from 5.6.1 I think). In perl 5.005 you could use something like $needle="a"; $haystack=~/\Q$needle/i; $index= pos($haystack)-length($needle) or $needle="a"; $haystack=~/\Q$needle/i; $index= length($`); but note that $` makes perl slower.

    Originally posted as a Categorized Answer.