For the latest version of JMP Help, visit JMP.com/help.


JSL Syntax Reference > JSL Functions > Character Pattern Functions
Publication date: 04/30/2021

Character Pattern Functions

See Pattern Matching in the Scripting Guide for more detailed information on constructing and using pattern matching expressions.

Pat Abort()

Description

Constructs a pattern that immediately stops the pattern match. The matcher does not back up and retry any alternatives. Conditional assignments are not made. Immediate assignments that were already made are kept.

Returns

0 when a match is stopped.

Argument

none

Pat Altern(pattern1, <pattern 2, ...>)

Description

Constructs a pattern that matches any one of the pattern arguments.

Returns

A pattern.

Argument

One or more patterns.

Pat Any("string")

Description

Constructs a pattern that matches a single character in the argument.

Returns

A pattern.

Argument

string

a string.

Pat Arb()

Description

Constructs a pattern that matches an arbitrary string. It initially matches the null string. It then matches one additional character each time the pattern matcher backs into it.

Returns

A pattern.

Argument

none

Example

p = "the beginning" + Pat Arb() >? stuffInTheMiddle + "the end";
Pat Match( "in the beginning of the story, and not near the end, there are three bears", p );
Show( stuffInTheMiddle );

stuffInTheMiddle = " of the story, and not near "

Pat Arb No(pattern)

Description

Constructs a pattern that matches zero or more copies of pattern.

Returns

A pattern.

Argument

pattern

a pattern to match against.

Example

adjectives = "large" | "medium" | "small" | "warm" | "cold" | "hot" | "sweet";
rc = Pat Match( "I would like a medium hot, sweet tea please",
               Pat Arbno( adjectives | Pat Any(", ") ) >> adj +
               ("tea" | "coffee" | "milk") );
Show( rc, adj );

rc = 1;

adj = " medium hot, sweet ";

Pat At(varName)

Description

Constructs a pattern that matches the null string and stores the current position in the source string into the specified JSL variable (varName). The assignment is immediate, and the variable can be used with expr() to affect the remainder of the match.

Returns

A pattern.

Argument

varName

the name of a variable to store the result in.

Example

p = ":" + Pat At( listStart ) + Expr(
	If( listStart == 1,
		Pat Immediate( Pat Len( 3 ), early ),
		Pat Immediate( Pat Len( 2 ), late )
	)
);
early = "";
late = "";
Pat Match( ":123456789", p );
Show( early, late );
early = "";
late = "";
Pat Match( "   :123456789", p );
Show( early, late );

First this is produced:

early = "123"

late = ""

and later this:

early = ""

late = "12"

Pat Break("string")

Description

Constructs a pattern that matches zero or more characters that are not in its argument; it stops or breaks on a character in its argument. It fails if a character in its argument is not found (in particular, it fails to match if it finds the end of the source string without finding a break character).

Returns

A pattern.

Argument

string

a string.

Pat Concat(pattern1, pattern2 <pattern 3, ...>)

Pattern1 + Pattern2 + ...

Description

Constructs a pattern that matches each pattern argument in turn.

Returns

A pattern.

Argument

Two or more patterns.

Pat Conditional(pattern, varName)

Description

Saves the result of the pattern match, if it succeeds, to a variable named as the second argument (varName) after the match is finished.

Returns

A pattern.

Arguments

pattern

a pattern to match against.

varName

the name of a variable to store the result in.

Example

type = "undefined";
rc = Pat Match(
	"green apples",
	Pat Conditional( "red" | "green", type ) + " apples"
);
Show( rc, type );

rc = 1;

type = "green";

Pat Fail()

Description

Constructs a pattern that fails whenever the matcher attempts to move forward through it. The matcher backs up and tries different alternatives. If and when there are no alternatives left, the match fails and Pat Match returns 0.

Returns

0 when a match fails.

Argument

none

Pat Fence()

Description

Constructs a pattern that succeeds and matches the null string when the matcher moves forward through it, but fails when the matcher tries to back up through it. It is a one-way trap door that can be used to optimize some matches.

Returns

1 when the match succeeds, 0 otherwise.

Argument

none

Pat Immediate(pattern, varName)

Description

Saves the result of the pattern match to a variable named as the second argument (varName) immediately.

Returns

A pattern.

Arguments

pattern

a pattern to match against.

varName

the name of a variable to store the result in.

Example

type = "undefined";
rc = Pat Match(
	"green apples",
	("red" | "green") >> type + " pears"
);
Show( rc, type );

rc = 0

type = "green"

Even though the match failed, the immediate assignment was made.

Pat Len(int)

Description

Constructs a pattern that matches n characters.

Returns

A pattern.

Argument

int

an integer that specifies the number of characters.

Pat Look Ahead(pattern, Boolean)

Description

A zero-width pattern match after the current position.

Arguments

pattern

the pattern.

Boolean

0 (the default) indicates a match. 1 designates a negative match or non-match.

Pat Look Behind(pattern, Boolean)

Description

A zero-width pattern match before the current position.

Arguments

pattern

the pattern.

Boolean

0 (the default) indicates a match. 1 designates a negative match or non-match.

Pat Match(SourceText, Pattern, <ReplacementText>, <NULL>, <ANCHOR>, <MATCHCASE>, <FULLSCAN>)

Description

Pat Match executes the Pattern against the SourceText. The pattern must be constructed first, either inline or by assigning it to a JSL variable elsewhere.

Returns

1 if the pattern is found, 0 otherwise.

Arguments

SourceText

A string or string variable that contains the text to be searched.

Pattern

A pattern or pattern variable that contains the text to be searched for.

ReplacementText

Optional string that defines text to replace the pattern in the source text.

NULL

A placeholder for the third argument if ANCHOR, MATCHCASE, or FULLSCAN are necessary and there is no replacement text.

ANCHOR

Optional command to start the pattern match to the beginning of the string. The following match fails because the pattern, “cream”, is not found at the beginning of the string:

Pat Match( "coffee with cream and sugar", "cream", NULL, ANCHOR );

MATCHCASE

Optional command to consider capitalization in the match. By default, Pat Match() is case insensitive.

FULLSCAN

Optional command to force Pat Match to try all alternatives, which uses more memory as the match expands. By default, Pat Match() does not use FULLSCAN, and makes some assumptions that allow the recursion to stop and the match to succeed.

Pat Not Any("string")

Description

Constructs a pattern that matches a single character that is not in the argument.

Returns

A pattern.

Argument

string

a string.

Pat Pos(int)

Description

Constructs patterns that match the null string if the current position is int from the left end of the string, and fail otherwise.

Returns

A pattern.

Argument

int

an integer that specifies a position in a string.

Pat R Pos(int)

Description

Constructs patterns that match the null string if the current position is int from the right end of the string, and fails otherwise.

Returns

A pattern.

Argument

int

an integer that specifies a position in a string.

Pat R Tab(int)

Description

Constructs a pattern that matches up to position n from the end of the string. It can match 0 or more characters. It fails if it would have to move backwards or beyond the end of the string.

Returns

A pattern.

Argument

int

an integer that specifies a position in a string.

Pat Regex("string")

Description

Constructs a pattern that matches the regular expression in the quoted string argument.

Returns

A pattern.

Argument

string

a string.

Pat Rem()

Description

Constructs a pattern that matches the remainder of the string. It is equivalent to Pat R Tab(0).

Returns

A pattern.

Argument

none

Pat Repeat(pattern, minimum, maximum, GREEDY|RELUCTANT)

Description

Matches pattern between minimum and maximum times.

Returns

A pattern.

Arguments

pattern

a pattern to match against.

minimum

An integer that must be smaller than maximum.

maximum

An integer that must be greater than minimum.

GREEDY|RELUCTANT

If GREEDY is specified, it tries the maximum first and works back to the minimum. If RELUCTANT is specified, it tries the minimum first and works up to the maximum.

Notes

Pat Arbno(p) is the same as Pat Repeat(p, 0, infinity, RELUCTANT)

Pat Repeat(p) is the same as Pat Repeat(p, 1, infinity, GREEDY)

Pat Repeat(p, n) is the same as Pat Repeat(p, n, infinity, GREEDY)

Pat Repeat(p, n, m) is the same as Pat Repeat(p, n, m, GREEDY)

Pat Span("string")

Description

Constructs a pattern that matches one or more (not zero) occurrences of characters in its argument. It is greedy; it always matches the longest possible string. It fails rather than matching zero characters.

Returns

A pattern.

Argument

string

a string.

Pat String("string")

Description

Constructs a pattern that matches its string argument.

Returns

A pattern.

Argument

string

a string.

Pat Succeed()

Description

Constructs a pattern that always succeeds, even when the matcher backs into it. It matches the null string.

Returns

1 when the match succeeds.

Argument

none

Pat Tab(int)

Description

Constructs a pattern that matches forward to position int in the source string. It can match 0 or more characters. It fails if it would have to move backwards or beyond the end of the string.

Returns

A pattern.

Argument

int

an integer that specifies a position in a string.

Pat Test(expr)

Description

Constructs a pattern that succeeds and matches the null string if expr is not zero and fails otherwise.

Returns

A pattern.

Argument

expr

An expression.

Note

Usually the argument is wrapped with expr() because the test needs to be made on the current value of variables set by Pat Immediate, Pat Conditional, and Pat At. Without expr, the test is based on values that were known when the pattern was constructed, which means the test always succeeds or always fails at pattern execution time, which is probably not what you want.

Example

nCats = 0;
whichCat = 3;
string = "catch a catnapping cat in a catsup factory";
rc = Pat Match(
	string,
	"cat" + Pat Test(
		Expr(
			nCats = nCats + 1;
			nCats == whichCat;
		)
	),
	"dog"
);
Show( rc, string, nCats );

rc = 1

string = "catch a catnapping dog in a catsup factory"

nCats = 3

Regex Match(source, pattern, <replacement>|<MATCHCASE>, <NULL>)

Description

Executes the pattern match in pattern against the quoted source string.

Returns

A pattern.

Required Arguments

source

a string.

pattern

a pattern.

Optional Arguments

replacement

The string that specifies the text to replace the source with.

MATCHCASE

The search is case insensitive unless you specify MATCHCASE.

NULL

Indicates that the expression contains MATCHCASE but you don’t want to specify a replacement.

Examples

Regex Match(
	"person=Fred id=77 friend= favorite=tea", // source
	"(\w+)=(\S*) (\w+)=(\S*) (\w+)=(\S*) (\w+)=(\S*)" // pattern
);

{"person=Fred id=77 friend= favorite=tea", "person", "Fred", "id", "77", "friend", "", "favorite", "tea"}

 
// case-insensitive, no replacement
Regex Match( "beliEve", "([aeiou])(.*?)(\1)" );

{"eliE", "e", "li", "E"}

// case-sensitive, no replacement
Regex Match( "beliEve", "([aeiou])(.*?)(\1)", NULL, MATCHCASE );

{"eliEve", "e", "liEv", "e"}

Want more information? Have questions? Get answers in the JMP User Community (community.jmp.com).