s = "Inside tags, you will find <tag>multiple</tag>" s += "<tag>pieces</tag>of <tag>content</tag>." m = matchAll(s, "<tag>(.*?)</tag>") for i in range(len(m)): print("Found '" + m[i] + "' inside a tag.") # Prints to the console: # "Found 'multiple' inside a tag." # "Found 'pieces' inside a tag." # "Found 'content' inside a tag."
This function is used to apply a regular expression to a piece of text, and return a list of matching groups (elements found inside parentheses) as a two-dimensional String array. If there are no matches, a null value will be returned. If no groups are specified in the regular expression, but the sequence matches, a two dimensional array is still returned, but the second dimension is only of length one.
To use the function, first check to see if the result is null. If the result is null, then the sequence did not match at all. If the sequence did match, a 2D array is returned.
If there are groups (specified by sets of parentheses) in the regular expression, then the contents of each will be returned in the array. Assuming a loop with counter variable i, element [i] of a regular expression match returns the entire matching string, and the match groups start at element [i] (the first group is [i], the second [i], and so on).
The syntax can be found in the reference for Java's Pattern class. For regular expression syntax, read the Java Tutorial on the topic.
Updated on Mon Sep 21 15:53:25 2020.
If you see any errors or have comments, please let us know.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License