Re: How can I find the first position of a substring case-insensitively?
by eg (Friar) on Dec 02, 2000 at 11:55 UTC
|
| [reply] |
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 | [reply] [d/l] [select] |
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
| [reply] [d/l] |
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
|
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.
| [reply] [d/l] |
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. | [reply] [d/l] |
Re: How can I find the first position of a substring case-insensitively?
by Anonymous Monk on Dec 05, 2000 at 09:01 UTC
|
| [reply] |
Re: How can I find the first position of a substring case-insensitively?
by tektsu (Acolyte) on Dec 06, 2000 at 23:44 UTC
|
| [reply] |
Re: How can I find the first position of a substring case-insensitively?
by tenatious (Beadle) on Dec 05, 2000 at 23:19 UTC
|
$b = index(lc($mystr),"a");
| [reply] [d/l] |
|
|
my $haystack = "foobar";
my $needle = "Bar";
$haystack =~ /($needle)/gi;
print ((pos $haystack) - (length $needle));
| [reply] [d/l] |
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. | [reply] [d/l] |
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. | [reply] [d/l] [select] |
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. | [reply] [d/l] [select] |
Re: How can I find the first position of a substring case-insensitively?
by Anonymous Monk on Dec 10, 2000 at 03:29 UTC
|
| [reply] |
Re: How can I find the first position of a substring case-insensitively?
by ambrus (Abbot) on Oct 01, 2003 at 12:55 UTC
|
| [reply] [d/l] [select] |
Re: How can I find the first position of a substring case-insensitively?
by ambrus (Abbot) on Oct 01, 2003 at 12:56 UTC
|
| [reply] [d/l] [select] |