该帮助的版本不再更新,请参见https://www.jmp.com/support/help/zh-cn/15.2 获取最新的版本.


Regex Match() returns an empty list with zero elements if the match fails. If the match succeeds, the first list is the text of the entire match (backreference 0). The second list is the text that matches backreference 1, and so on.
Unlike Regex(), Regex Match() is case insensitive. Include MATCHCASE for a case-sensitive match. Include NULL if you want to match case but there is no replacement text.
"person=Fred id=77 friend= favorite=tea",
"(\w+)=(\S*) (\w+)=(\S*) (\w+)=(\S*) (\w+)=(\S*)"
The \w+ matches one or more word characters. The \S* matches zero or more characters that are not spaces. In the resulting JSL list, the field names (person, id, friend, favorite) and their corresponding values (Fred, 77, "", tea) are separate strings.
If the first argument to Regex Match() is a variable and a third argument specifies the replacement value, the matched text is replaced in the variable.
Regex() and Regex Match() match a pattern in a given string but return different results. To transforms your string into another string, use Regex(). To identify the substrings that match specific parts of the pattern, use Regex Match().
This example shows the efficiency of Regex Match() compared to Regex(). The source is a list of six strings. The goal is to extract portions of those six strings into the subject, verb, and object columns of a data table (图 6.4).
图 6.4 Final Data Table
source = {"the cat ate the chicken", "the dog chased the cat", "did ralph like mary", "the girl pets the dog",
"these words are strange", "the cat was chased by the dog"};
dt = New Table( "English 101", // create the data table
New Column( "subject", character ),
New Column( "verb", character ),
New Column( "object", character )
For( i = 1, i <= N Items( source ), i++,
matchList = Regex Match(
".*?(cat|dog|ralph|girl).*?(ate|chased|like|pets).*?(chicken|cat|mary|dog)"
// scan each string, match zero or more characters
If( N Items( matchList ) > 0,
dt << Add Rows( 1 );
dt:subject = matchList[2]; // match the first open parenthesis
dt:verb = matchList[3]; // match the second open parenthesis
dt:object = matchList[4]; // match the third open parenthesis
Regex Match() returns {"the cat was chased by the dog", "cat", "chased", "dog"} in a single try with each answer in a separate string. Compare this example to a similar one using Regex(), which returns one answer at a time and builds the final string using backreferences.
For( i = 1, i <= N Items( source ), i++,
s = Regex( source[i], ".*?(cat|dog|ralph|girl).*?(ate|chased|like|pets).*?(chicken|cat|mary|dog)", "\1" ); // match an item in the first group
v = Regex( source[i], ".*?(cat|dog|ralph|girl).*?(ate|chased|like|pets).*?(chicken|cat|mary|dog)", "\2" ); // match an item in the second group
o = Regex( source[i], ".*?(cat|dog|ralph|girl).*?(ate|chased|like|pets).*?(chicken|cat|mary|dog)", "\3" ); // match an item in the third group
If( !Is Missing( s ) & !Is Missing( v ) & !Is Missing( o ),
dt << Add Rows( 1 );
dt:subject = s; // return the match for \1
dt:verb = v; // return the match for \2
dt:object = o; // return the match for \3