Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Answer: How can I find the first position of a substring case-insensitively?

by ambrus (Abbot)
on Oct 01, 2003 at 12:43 UTC ( #295598=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How can I find the first position of a substring case-insensitively? - Answer contributed by ambrus

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

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2016-09-27 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (499 votes). Check out past polls.