wildcardPattern

Pattern that matches any run of characters.

Category: Language Fundamentals > Data Types

Syntax

pat = wildcardPattern

Matches any number of characters, including none.

pat = wildcardPattern(n)

Matches exactly n characters.

pat = wildcardPattern(min, max)

Matches between min and max characters.

Description

The wildcard stands in for text you do not care about. It is typically placed between literal pieces, as in "report_" + wildcardPattern + ".csv", so that the whole pattern describes a family of names. The match is lazy: it takes as few characters as it needs for the rest of the pattern to succeed.

With matches the entire text has to fit the pattern, which makes it the usual way to test file names.

Input arguments

n
Exact number of characters.
min, max
Limits on how many characters the wildcard may span.

Output arguments

pat
A pattern object, combined with literal text using +.

Example

Test file names

names = ["report_2024.csv" "report_2024.txt" "data.csv"];
matches(names, "report" + wildcardPattern + ".csv")
startsWith(names, wildcardPattern(2) + "ta")
Output
ans = 1×3 logical array

   1   0   0
ans = 1×3 logical array

   0   0   1

See also

whitespacePattern pattern matches startsWith contains