regexpLastIndex()

The function "regexpLastIndex()" provides pattern matching via regular expressions. The function searches for a match of the specified character(s) starting at the end of the string.

Synopsis

int regexpLastIndex(string regexp, string line [,mapping options]);

Parameter Description
rexexp

The regular expression. For allowed characters, see Qt - Regular Expression - Captured Texts under "Characters and Abbreviations for Sets of Characters".

An example would be e.g. "[a-z|A-Z]+" .

This example searches for letters from a-z and A-Z and the + sign searches the letter one or more times meaning the + sign stands for at least one-time repetition.

line The string that is checked.
options

The options applied to the search. For the mapping the following keys can be used:

  • startPosition: Set the start position for the search. The default is 0 (int), the beginning of the string. -1 means that the function searches for the characters starting at the last character of the string. With -2, the function searches starting at the second to last character, etc.
  • caseSensitive: Set Case Sensitive matching. The default is false(bool).
  • minimal: Set minimal matching (Default: bool false). For an example of minimal matching, see regexpSplit().

Return value

  • The function returns the position of the first match of the searched character(s). The index starts at 0.
  • The function returns -1 if no match was found.
  • The function returns -2 when the regular expression contains an error. Errors can be retrieved by using the function getLastError().
  • The function returns -3 when the function contains wrong arguments.

Error

See above

Details

The function "regexpLastIndex()" provides pattern matching via regular expressions. The function searches for a match of the specified character(s) starting at the end of the string.

The first example containing "( [hH])+" searches for the letter h or H with a preceding blank starting from the end of the text. The function returns the first match. The first match with the letter H or h with a preceding blank is returned. The first match from the end of the string is [blank]Hannah. The index is calculated starting from 0. Thus, the example returns 22.

The second example with "[hH]+" searches for letters h or H without a blank. The first match starting from the end of the text with h or H without blank is Ellah. The index is calculated starting from 0. Thus, the example returns 62.

The third example searches for the letters h or H without blank starting at the character next to the last character at the end of the string ("startPosition",-2). The first match from the end is Mariah. The index is calculated starting from 0. Thus, the example returns 44.

main(mapping event)
{
  string line ="Heinrich, Hans, Heike, Hannah, Martin, Mariah, Mia, Eric, Ellah";
  int i = 0;
  i = regexpLastIndex("( [hH])+",line,makeMapping("startPosition",-1));
  DebugN("ReturnValue:", i);
  i = regexpLastIndex("[hH]+",line,makeMapping("startPosition",-1));
  DebugN("ReturnValue:", i);
  i = regexpLastIndex("[hH]+",line,makeMapping("startPosition",-2));
  DebugN("ReturnValue:", i);
}

Assignment

Regular expressions