Next Previous Contents

69. string_match_nth

Synopsis

Get the result of the last call to string_match

Usage

(Integer_Type, Integer_Type) string_match_nth(Integer_Type nth)

Description

The string_match_nth function returns two integers describing the result of the last call to string_match. It returns both the offset into the string and the length of characters matches by the nth submatch.

By convention, nth equal to zero means the entire match. Otherwise, nth must be an integer with a value 1 through 9, and refers to the set of characters matched by the nth regular expression enclosed by the pairs \(, \).

Example

Consider:

     variable matched, pos, len;
     matched = string_match("hello world", "\\([a-z]+\\) \\([a-z]+\\)", 1);
     if (matched) (pos, len) = string_match_nth(2);
This will set matched to 1 since a match will be found at the first position, pos to 6 since w is offset 6 characters from the beginning of the string, and len to 5 since "world" is 5 characters long.
Notes

The position offset is not affected by the value of the offset parameter to the string_match function. For example, if the value of the last parameter to the string_match function had been 3, pos would still have been set to 6.

Note also that string_match_nth returns the offset from the beginning of the string and not the position of the match.

See Also

string_match


Next Previous Contents