ZetCode

Perl index 函数

最后修改于 2025 年 4 月 4 日

Perl 的 index 函数在字符串中搜索子字符串。它返回第一个匹配项的位置,如果未找到则返回 -1。

index 区分大小写,并且可以从特定位置开始搜索。它对于 Perl 中的字符串操作和解析任务至关重要。

基本的 index 用法

使用 index 最简单的方法是在字符串中查找子字符串。

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str = "Hello world";
my $pos = index($str, "world");

print "Substring found at position: $pos\n";

我们在 "Hello world" 中搜索 "world"。该函数返回子字符串开始的位置(6)。

$ ./basic.pl
Substring found at position: 6

处理未找到的情况

当字符串中未找到子字符串时,index 返回 -1。

notfound.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str = "Perl programming";
my $pos = index($str, "Python");

if ($pos == -1) {
    print "Substring not found\n";
} else {
    print "Found at position: $pos\n";
}

这演示了如何处理子字符串不存在的情况。在使用 index 时,请始终检查 -1。

$ ./notfound.pl
Substring not found

从指定位置开始搜索

index 可以从字符串中的特定位置开始搜索。

position.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str = "apple orange apple banana";
my $pos1 = index($str, "apple");
my $pos2 = index($str, "apple", $pos1 + 1);

print "First apple at: $pos1\n";
print "Second apple at: $pos2\n";

我们找到第一个 "apple",然后从该位置之后开始再次搜索。这种技术对于查找子字符串的多个匹配项非常有用。

$ ./position.pl
First apple at: 0
Second apple at: 12

区分大小写

index 区分大小写,需要精确的字符匹配。

case.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str = "Perl Language";
my $pos1 = index($str, "perl");
my $pos2 = index($str, "Perl");

print "Lowercase search: $pos1\n";
print "Exact case search: $pos2\n";

第一次搜索由于大小写不匹配而失败(-1),而第二次成功。对于不区分大小写的搜索,请先将字符串转换为相同的大小写。

$ ./case.pl
Lowercase search: -1
Exact case search: 0

查找所有匹配项

我们可以使用一个带有 index 的循环来查找所有子字符串的匹配项。

all.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str = "mississippi";
my $sub = "iss";
my $pos = -1;
my @positions;

while (($pos = index($str, $sub, $pos + 1)) != -1) {
    push @positions, $pos;
}

print "Positions: @positions\n";

此脚本查找 "mississippi" 中 "iss" 的所有起始位置。我们在每次找到后增加搜索位置,以避免无限循环。

$ ./all.pl
Positions: 1 4

将 index 与子字符串一起使用

index 可以高效地搜索多字符子字符串。

substring.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $str = "The quick brown fox jumps over the lazy dog";
my $word = "fox";
my $pos = index($str, $word);

if ($pos != -1) {
    print "Found '$word' at position $pos\n";
    print "Context: " . substr($str, $pos - 5, length($word) + 10) . "\n";
}

我们找到一个单词并显示其周围的上下文。这表明 index 可以与其他字符串函数结合使用,以实现强大的文本处理。

$ ./substring.pl
Found 'fox' at position 16
Context: quick brown fox jumps

性能注意事项

与正则表达式相比,index 针对速度进行了优化。

performance.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;
use Benchmark qw(cmpthese);

my $str = "a" x 1000 . "needle" . "a" x 1000;

cmpthese(-1, {
    index => sub { index($str, "needle") != -1 },
    regex => sub { $str =~ /needle/ },
});

此基准测试将 index 与正则表达式匹配进行简单子字符串搜索进行比较。对于精确匹配,index 通常更快。

$ ./performance.pl
          Rate regex index
regex  65436/s    --  -57%
index 151515/s  132%    --

最佳实践

来源

Perl index 文档

本教程介绍了 Perl 的 index 函数,并通过实际示例演示了其在常见字符串搜索场景中的用法。

作者

我叫 Jan Bodnar,是一名充满热情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。至今,我已创作了超过 1,400 篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出 所有 Perl 教程