Main Content

asManyOfPattern

Match pattern as many times as possible

Since R2020b

Description

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

example

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

example

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

example

Examples

collapse all

Use asManyOfPattern to match as many individual letters as possible between two instances of "b".

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

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

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

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

Use asManyOfPattern to match as many 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 many 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 matched letters.

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

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

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

Use asManyOfPattern to match as many 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 matches as many 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 a maximum of four matched letters.

txt = "bb bab babab babaaabab";
pat = "b" + asManyOfPattern("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