asFewOfPattern

Pattern that matches another pattern repeatedly, taking as few repetitions as possible.

Category: Language Fundamentals > Data Types

Syntax

newPat = asFewOfPattern(pat)

Matches pat one or more times, lazily.

newPat = asFewOfPattern(pat, minLength)

Requires at least minLength repetitions.

Description

The result repeats pat, but when it is part of a larger pattern it stops as soon as the rest of the pattern can match. It is the lazy counterpart of asManyOfPattern, which takes as much text as it can.

On its own, with nothing after it, the match is the shortest allowed one, so minLength decides how much text is taken each time. Combine it with other patterns using +.

Input arguments

pat
A pattern, or text that is converted to a pattern.
minLength
Minimum number of repetitions.

Output arguments

newPat
A pattern object.

Example

Greedy and lazy in context

extract('xabbby', 'a' + asManyOfPattern('b'))
extract('xabbby', 'a' + asFewOfPattern('b') + 'y')
Output
ans = "abbb"
ans = "abbby"

The lazy part still has to reach the y, so it takes all three b characters.

Notes

Used alone or at the end of a pattern without minLength, MatLivre may match fewer repetitions than expected, so place it before another pattern or pass minLength.

See also

asManyOfPattern wildcardPattern optionalPattern extract pattern