regexpIndex()
The function "regexpIndex()" provides pattern matching via regular expressions. The function searches for a match of the specified character starting from the given position.
Synopsis
int regexpIndex(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]+" This example searches for the character a. 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 = Start 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. The index starts at 0. This means that the first match of the character s in the word "This" would be the position 3.
-
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 "regexpIndex()" provides pattern matching via regular expressions. The function searches for a match of the specified character starting from the given position.
The first example searches for the letter n starting from the position 0 (Start of the text). The function returns the first match of the letter. The first match of the letter a is a (This is a text with a). The index is calculated starting from 0. Thus, the example returns 10.
The second example searches for the letter n starting from the end of the text. The function returns the first match of the letter. The first match of the letter a starting from the end of the text is a (This is a text with a). The index is calculated starting from 0. Thus, the example returns 21.
main(mapping event)
{
int i = 0;
i = regexpIndex("[a]+", "This is a text with a", makeMapping("startPosition", 0));
DebugN("Position of 'a' in This is a text with a. Searched from position 0:", i);
i = regexpIndex("[a]+", "This is a text with a", makeMapping("startPosition", -1));
DebugN(""Position of 'a' in This is a text with a. Searched from end of string:", i);
}
Assignment
Regular expressions