Go to: Synopsis. Return value. Related. MEL examples.

Synopsis

match string string

match is NOT undoable, NOT queryable, and NOT editable.

Returns the portion of the second string argument which matches the regular expression in the first string argument. The regular expression matches consecutive characters. If there is no match the empty string ("") is returned. Regular expressions are a standard syntax for specifying search patterns. This reference page acts as a brief overview of regular expressions. Regular expressions are strings that contain special characters which define matching criteria. The simplest expression is just a string (see example 1 below). The basic building blocks of a regular expression are as follows:
. Matches any single character
* Match zero or more occurances of the preceeding expression
+ Match one or more occurances of the preceeding expression
^ Matches (anchors) the expression to the start of a line
$ Matches (anchors) the expression to the end of a line
\ escape character. Use this in front of a special character (e.g. '*') when you wish to match the actual character. Note that MEL strings resolve control characters that use '\' when they are first parsed. So, to use a '\' character in an expression, you must escape it (e.g. "\\") so that it is passed through to match.
[...] Matches any one of the enclosed characters. A pair of characters separated by - matches any character lexically between the pair, inclusive. If the first character following the opening "[ " is a "^" any character not enclosed is matched. A - can be included in the character set by putting it as the first or last character.
(...) Used to group part of an expression together

Return value

string Matched string

Related

gmatch, strcmp

MEL examples

// Example 1: a simple string match
//
match "this" "this is a test";
// Result: this //

// Example 2: using '*' and '+'
//
match "a*b*" "abbcc";
// Result: abb //
match "a*b*" "bbccc";
// Result: bb //

match "a+b+" "abbcc";
// Result: abb //
// The following will fail because there has to be at least one "a"
match "a+b+" "bbccc";

// Example 3: anchoring to the start and end of the line
//
// This matches "the", only if it is at the start of the line
match "^the" "the red fox";
// Result: the //
// The following fails
match "^the" "chase the red fox";
// This matches "fox", only if it is at the end of the line
match "fox$" "the red fox";
// The following fails
match "fox$" "the red fox hides";

// Example 4: matching ranges of characters
//
match "[0-9]+" "sceneRender019.iff";
// Result: 019 //

// Example 5: using ()
//
match "(abc)+" "123abcabc456";
// Result: abcabc //

// Example 6: the escape charater
//
// as mentioned above, MEL parses '\' escape characters in strings.  So,
// to pass an actual '\' character through to match, you must escape it.
//
// The following regular expression is really "\^.", but we have to escape
// the '\'.
//
match("\\^.", "ab^c");
// Result: ^c
//