Name

match()

Examples
s = "Inside a tag, you will find <tag>content</tag>."
m = match(s, "<tag>(.*?)</tag>")
print("Found '%s' inside the tag" % m[1])
# Prints to the console:
# "Found 'content' inside the tag."
s1 = "Have you ever heard of a thing called fluoridation. "
s1 += "Fluoridation of water?"
s2 = "Uh? Yes, I-I have heard of that, Jack, yes. Yes."
m1 = match(s1, "fluoridation")
if m1 != None:  # If not none, then a match was found
    # This will print to the console, since a match was found.
    print("Found a match in '" + s1 + "'")
else:
    print("No match found in '" + s1 + "'")
m2 = match(s2, "fluoridation")
if m2 != None:
    print("Found a match in '" + s2 + "'")
else:
    # This will print to the console, since no match was found.
    print("No match found in '" + s2 + "'")
Description This function is used to apply a regular expression to a piece of text, and return matching groups (elements found inside parentheses) as a String array. If there are no matches, a None value will be returned. If no groups are specified in the regular expression, but the sequence matches, an array of length 1 (with the matched text as the first element of the array) will be returned.

To use the function, first check to see if the result is None. If the result is None, then the sequence did not match at all. If the sequence did match, an 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. Element [0] of a regular expression match returns the entire matching string, and the match groups start at element [1] (the first group is [1], the second [2], 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.
Syntax
match(str, regexp)
Parameters
strString: the String to be searched
regexpString: the regexp to be used for matching
Related matchAll()
.split()
.join()
Updated on Tue Jul 11 06:52:24 2017.
Creative Commons License