Perl-rindex

提供:Dev Guides
移動先:案内検索

Perl rindex関数

説明

この関数はindexと同様に動作しますが、STRで最後に出現したSUBSTRの位置を返す点が異なります。 POSITIONが指定されている場合、その位置で、またはその位置の前で最後に出現したものを返します。

構文

以下は、この関数の簡単な構文です-

rindex STR, SUBSTR, POSITION

rindex STR, SUBSTR

戻り値

この関数は失敗するとundefを返します。それ以外の場合、最後に発生した位置を返します。

以下は、その基本的な使用法を示すコード例です-

#!/usr/bin/perl -w

$pos = rindex("abcdefghijiklmdef", "def");
print "Found position of def $pos\n";

# Use the first position found as the offset to the
# next search.
# Note that the length of the target string is
# subtracted from the offset to save time.

$pos = rindex("abcdefghijiklmdef", "def", $pos-3 );
print "Found position of def $pos\n";

上記のコードが実行されると、次の結果が生成されます-

Found position of def 14
Found position of def 3