Reference for Processing (BETA) version 0136+. If you have a previous version, use the reference included with your software. If you see any errors or have any comments, let us know.

Name

match()

Examples
String s = "Inside a tag, you will find content.";
String[] m = match(s, "(\\S+)");
println("Found " + m[0] + " inside the tag.");
// Prints "Found content inside the tag." to the console

String s1 = "Have you ever heard of a thing called fluoridation. Fluoridation of water?";
String s2 = "Uh? Yes, I-I have heard of that, Jack, yes. Yes.";

String[] m1 = match(s1, "fluoridation");
if (m1 != null) {
  println("Found a match in '" + s1 + "'");  // This will print to the console.
} else {
  println("No match found in '" + s1 + "'");
}

String[] m2 = match(s2, "fluoridation");
if (m2 != null) {
  println("Found a match in '" + s2 + "'");
} else {
  println("No match found in '" + s2 + "'");  // This will print to the console.
}
Description The match() 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. No match will return null. If no groups are specified in the regexp, but the sequence matches, a zero length (non-null) array will be returned.

To use the function, first check to see if the result is null. If the result is null, then the sequence did not match. If the sequence did match, an array is returned. If there are groups (specified by sets of parentheses) in the regexp, then the contents of each will be returned in the array.

The syntax can be found in the reference for Java's Pattern class. For regular expression syntax, read the Java Tutorial on the topic.

Technical note: Some languages and APIs use element [0] of a regexp match to return the entire matching string, and start the match groups at element [1]. In the Processing implementation, matching groups start at index [0].
Syntax
match(str, regexp)
Parameters
str the String to be split
regexp the regexp to be used for matching
Usage Web & Application
Related split()
splitTokens()
join()
trim()
Updated on July 17, 2008 09:50:06pm PDT

Creative Commons License