Main Content

asFewOfPattern

Match pattern as few times as possible

Since R2020b

Description

newpat = asFewOfPattern(pat) creates a pattern that matches as few consecutive instances of pat as possible, including zero times.

example

newpat = asFewOfPattern(pat,minPattern) specifies a minimum number of consecutive instances to match with minPattern.

example

newpat = asFewOfPattern(pat,minPattern,maxPattern) specifies a minimum and a maximum number of consecutive instances to match. asFewOfPattern matches at least minPattern consecutive instances, but no more than maxPattern.

example

Examples

collapse all

Use asFewOfPattern to match as few individual letters as possible between two instances of "b".

Create txt as a string. Create a pattern, pat, that matches as few of the letters "a" or "b" as possible between two instances of the character "b".

txt = "bb bab babab babaaabab";
pat = "b" + asFewOfPattern("a"|"b") + "b";

Use replace to replace text matched by pat with the character "*".

replace(txt,pat,"*")
ans = 
"* * *ab *aaa*"

Use asFewOfPattern to match as few individual letters as possible between two instances of "b" but require at least three letters.

Create txt as a string. Create a pattern, pat, that matches as few of the letters "a" or "b" possible between two instances of the character "b", but specify that there must be a minimum of three matched letters.

txt = "bb bab babab babaaabab";
pat = "b" + asFewOfPattern("a"|"b",3) + "b";

Use replace to replace text matched by pat with the character "*".

replace(txt,pat,"*")
ans = 
"bb bab * *ab"

Use asFewOfPattern to match as few individual letters as possible between two instances of "b", but require at least three and no more than four letters.

Create txt as a string. Create a pattern, pat, that will match as few of the letters "a" or "b" as possible between two instances of the character "b", but specify that there must be a minimum of three and no more than four letters.

txt = "bb bab babab babaaabab";
pat = "b" + asFewOfPattern("a"|"b",3,4) + "b";

Use replace to replace text matched by pat with the character "*".

replace(txt,pat,"*")
ans = 
"bb bab * ba*ab"

Input Arguments

collapse all

Input pattern, specified as a pattern, string array, character vector, or cell array of character vectors.

Data Types: char | string | pattern | cell

Minimum number of consecutive instances to match, specified as a nonnegative integer scalar.

Data Types: single | double

Maximum number of consecutive instances to match, specified as a nonnegative integer scalar.

Data Types: single | double

Output Arguments

collapse all

Output pattern, returned as a pattern or an array of pattern objects.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2020b