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 http://doc.qt.io/qt-4.8/qregexp.html#capturedTexts 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 |
For the mapping the following keys can be used: startPosition: Set the start position for the search: (Default: int 0 = 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 next to last character etc. caseSensitive: Set Case Sensitive matching (Default: bool false). Set minimal matching (Default: bool false). for an example of minimal matching, see chapter 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
Description
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.
Example
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