Perl rindex Function

The rindex function is similar to index, but returns the position of the last occurrence of a substring in a string.

You may consider the rindex function like a reverse for the index function – right to left substring search. It returns the position of the last occurrence of a substring in a string, looking from right to left.

To find a substring in a string from right to left, you can use the regular expressions too, but using the rindex function is faster and easier to understand.

Try to avoid using regular expressions for simple string operations especially when some built-in functions specially designed for this purpose are available. The regex have to manipulate memory variables, so keep them for more sophisticated and complex operations.

Two syntax forms are available for the Perl rindex function:

rindex STR,SUBSTRING,POSITION
rindex STR,SUBSTRING
Here, the three arguments have the following meaning:
  • STR - the string you are searching
  • SUBSTRING - the substring whose position you want to locate or to search for
  • POSITION – if specified, is the rightmost position that can be returned, from that position the function will search from right to left looking for the substring to match

If you don’t provide the third argument, the rindex function will search for the substring beginning with the last character of the string and from right to left.

It will return the position of the first occurrence of the substring if the substring was found and -1 otherwise.

The parentheses are optional, it is your choice to use them or not. Still, there are cases when it is better to use them, in order to make your code more readable.

You can use the return value of the rindex function to find out if a substring is included in a string, looking backwards, i.e. from write to left. You can also supply a start position for your searching or let the Perl rindex function search from the rightmost character of the string.

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string scalar
my $textStr = "PDF::Create is a Perl module";
if((my $pos = rindex(lc $textStr, lc "perl")) >= 0) {
  print "I found it!\n";
}
Please note that at first we converted both the string and the substring in lowercase (by using lc function) in order to make our search case insensitive.

You can use the rindex function to find the last occurrence of a substring in a string because this function searches for a substring from right to left, beginning with the last character of the string (if the position from where to begin the search is not specified).

The following simple example shows you a way to do it:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
my $str  = 'What you see is what you get';
my $pos;
 
print "Found it at $pos position!\n"
     if(($pos = rindex(uc $str,'YOU')) > -1);
 
# Found it at 21 position!
To make the search case insensitive the uc function is used. The Perl rindex function returns the position of the last occurrence of the 'YOU' substring in the $str string. If an occurrence is found, the script will print the position of the substring in the string, otherwise it will do nothing.

You can search for all the occurrences of a substring in a string either by using index or rindex function. The example below looks for the positions of the slash character in a string by using the rindex function (you can use the same algorithm to search for a substring composed from more than one characters, though).

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string scalar
my $url = "http://www.mysite.com/rindex.html";
my $pos = length($url)-1;
 
print "/ found at positions: ";
 
while(1)
{
  $pos = rindex($url, "/", $pos);
  last if($pos < 0);
  print $pos--, " ";
}
 
print "\n";
I used the last keyword to finish the while loop iteration. If you run this code, you’ll get the output:
 
/ found at positions: 21 6 5