Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

Perl pos() function in Regex

The pos() function in Perl is used in conjunction with regular expressions. It returns or sets the offset position at which the next search would start for a particular string variable. This function is particularly useful when you want to perform global matches on a string without overwriting the global match iterator g.

Basics of pos()

  1. Getting the Position: The pos() function can retrieve the position after the last match:

    my $string = "hello world";
    $string =~ m/o/;
    print pos($string);  # Outputs "5" since the first 'o' is at position 4 and the next search would start at position 5
    
  2. Setting the Position: You can also set the position manually:

    my $string = "hello world";
    pos($string) = 7;
    

Using pos() with /g Modifier:

The pos() function is particularly useful with the /g (global) modifier in regex. When /g is used in a match (m//g), it remembers the position where the last match stopped and picks up from there the next time a m//g operation is executed against the string.

my $string = "ababab";

while ($string =~ /a/g) {
    print "Found 'a' at position ", pos($string) - 1, "\n";
}

Output:

Found 'a' at position 0
Found 'a' at position 2
Found 'a' at position 4

/g and pos() with while loop:

A common usage is to use pos() in conjunction with a while loop to capture multiple overlapping matches:

my $string = "aaabaaa";
while ($string =~ /aa/g) {
    print "Found 'aa' ending at position ", pos($string), "\n";
    pos($string) -= 1;  # Move position back one character for overlapping matches
}

Output:

Found 'aa' ending at position 2
Found 'aa' ending at position 3
Found 'aa' ending at position 4
Found 'aa' ending at position 6

Resetting pos():

If you want to reset the search position so that /g starts from the beginning again on the next match, you can use the reset function:

my $string = "hello";
$string =~ /e/g;
print pos($string), "\n";  # 2

reset pos($string);
print pos($string), "\n";  # undef

Conclusion:

The pos() function provides a way to manage and manipulate the position of the global regex search on a string in Perl. This allows for flexibility, especially when handling overlapping matches or when wanting to restart a search from a specific position.

    my $string = "abcde";
    $string =~ /b/;
    my $position = pos($string);
    print "Match position: $position\n";  # Outputs: 1
    
      my $string = "abcde";
      $string =~ /b/;
      my $position = pos($string);
      print "Capture position: $position\n";  # Outputs: 1
      
        my $string = "abcde";
        $string =~ /b/;
        my $position = pos($string);
        print "Initial position: $position\n";  # Outputs: 1
        
        pos($string) = 3;  # Set the position
        $string =~ /d/;
        $position = pos($string);
        print "New position: $position\n";  # Outputs: 4
        
          my $string = "abcabc";
          while ($string =~ /a/g) {
              my $position = pos($string);
              print "Match at position: $position\n";
          }
          
            my $string = "abcabc";
            $string =~ /a/g;
            pos($string) = 3;  # Skip the first match
            $string =~ /a/g;
            my $position = pos($string);
            print "New match position: $position\n";  # Outputs: 4
            
              my $string = "abc123";
              $string =~ /(\d+)/;
              my $position = pos($string);
              print "Matched digits at position: $position\n";  # Outputs: 4