possessivePattern

Pattern that matches as much as possible and never gives characters back.

Category: Language Fundamentals > Data Types

Syntax

p = possessivePattern(pat)

Matches pat greedily with no backtracking.

Description

Ordinary repeating patterns can back off: in lettersPattern + "b", the letters part gives up the final b so that the whole pattern can match. A possessive pattern keeps everything it took. As a result possessivePattern(lettersPattern) + "b" can never match, because the letters already consumed the b.

Possessive matching is mostly useful for speed and for forbidding unwanted alternative matches. It is the counterpart of the possessive quantifiers (*+, ++) of regular expressions.

Input arguments

pat
A pattern or text.

Output arguments

p
A pattern object.

Example

matches("aab", lettersPattern + "b")
matches("aab", possessivePattern(lettersPattern) + "b")
Output
ans = logical
   1
ans = logical
   0

See also

pattern lettersPattern optionalPattern regexpPattern matches