In a regular expression, parentheses group parts of the expression so they can be treated as a unit:
The expression "Set( value)?" matches "Set" and "Set value", but not "Set valu".
The text that each group matches is also captured for later use. When the expression
"(\d+)\D*(\d+)" is applied to the string "10 dogs chased 25 cats"
the numbers 10 and 25 are saved for later use by the matching engine. These saved groups are numbered in order of the opening parentheses in the expression. So "10" would be saved as group1 and "25" would be saved as group2.
Tasker's implementation of Search and Replace, unfortunately, discards the saved groups as soon as the action is completed.
This task will find matching text and save the groups in a global array. It is designed to be called as a subroutine using the Perform Task action.
- %par1 is the string to search
- %par2 is the regular expression to use.
Both must be set the first time the task is called.
The task will perform various actions depending on the state of %par1 and %par2:
- Both set - Find the first match in %par1 using expression %par2
- Both clear - Find the next match in the saved string using the saved expression
- Only %par1 set - Find the first match in new string %par1 using the saved expression
- Only %par2 set - Find the first match in the saved string using new expression %par2
The task returns true if a match was found and false if a match could not be found.
If there is an error, probably due to a bad expression, the task returns nothing and the Return Value Variable is not set.
The results are stored in several global variables:
- rxGroup - The text that matched the whole expression
- rxStart - The index in the string where rxGroup starts
- rxEnd - The index in the string where RxGroup ends + 1
- rxGroup() - The text of the groups as an array
- rxStart() - The index in the string where each group starts
- rxEnd() - The index in the string where each group ends + 1
Note: In Java, indexes start at zero. The first character of the string is at index 0, the second at index 1, etc.
You can have as many groups as you need and they can be nested. For example, applying the expression "(a(b(c)))" to the string "abc" would result in three captured groups: "abc", "bc" and "c".
Note: the task creates a global Java object, rMatcher. To save on memory this object should be deleted when no longer needed.