Main Content

wildcardPattern

Matches as few characters of any type

Since R2020b

Description

example

pat = wildcardPattern creates a pattern that matches as few of any characters as needed, including zero characters. wildcardPattern is lazy, meaning that if used on its own without any inputs it matches empty strings ("") before or after characters in strings.

example

pat = wildcardPattern(N) creates a pattern that matches exactly N characters.

example

pat = wildcardPattern(minCharacters,maxCharacters) matches at least minCharacters and no more than maxCharacters. inf is a valid value for maxCharacters. wildcardPattern matches a quantity of characters as close to minCharacters as possible.

example

pat = wildcardPattern(___,"Except",exceptPat) specifies exceptions to exclude from matching. wildcardPattern matches any character except those specified by the pattern exceptPat.

Examples

collapse all

The function wildcardPattern is lazy, which means it matches as few characters of any kind as needed, even zero.

Extract a wildcardPattern from a string. There are empty strings between any two consecutive characters in a string, as well as before the first character and after the last. That is why extract matches and extracts five empty strings from this string.

txt = "lazy";
emptyStr = extract(txt,wildcardPattern)
emptyStr = 5x1 string
    ""
    ""
    ""
    ""
    ""

strlength(emptyStr)
ans = 5×1

     0
     0
     0
     0
     0

If wildcardPattern is bounded by other patterns, it matches a minimum number of characters for the pattern expression to match.

txt = "aa aba abaa a a123a a!?.a";
pat = "a" + wildcardPattern + "a";
extract(txt,pat)
ans = 6x1 string
    "aa"
    "aba"
    "aba"
    "a a"
    "a123a"
    "a!?.a"

Use wildcardPattern to match individual characters of all types.

Read the text from Shakespeare's Sonnets with the fileread function and convert it to string.

sonnets = string(fileread('sonnets.txt'));
strip(extractBetween(sonnets,"Shakespeare","But"))
ans = 
    "I
     
       From fairest creatures we desire increase,
       That thereby beauty's rose might never die,"

Create a pattern, pat, that matches one character of any type. Extract the pattern. Convert the string array characters to categorical so that it is a valid input for histogram. Display the occurrences of each character using histogram.

pat = wildcardPattern(1);
characters = extract(sonnets,pat);
characters = lower(characters);
characters = categorical(characters);
histogram(characters)

Create txt as a string. Create a pattern that matches a white space followed by two to three wildcard characters followed by a letter. Extract the pattern from txt.

txt = "1a 23b 456c 7890d";
pat = " " + wildcardPattern(2,3) + lettersPattern;
extract(txt,pat)
ans = 2x1 string
    " 23b"
    " 456c"

Separate a comma-separated list by using wildcardPattern.

Create txt as a string. Build a pattern using wildcardPattern that matches as many characters of any type except "," followed by whitespace characters. Extract the pattern.

txt = "1 fish, 2 Fish, [1,0,0] fish, [0,0,1] fish";
exceptPat = "," + whitespacePattern;
pat = asManyOfPattern(wildcardPattern(1,inf,"Except",exceptPat),1);
extract(txt,pat)
ans = 4x1 string
    "1 fish"
    " 2 Fish"
    " [1,0,0] fish"
    " [0,0,1] fish"

Input Arguments

collapse all

Number of characters to match, specified as a nonnegative integer scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

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

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Exception to wildcard matching, specified as a pattern object, character vector or string scalar. wildcardPattern will match any character except matches to this specified exception.

Example: pat = wildcardPattern("except",digitsPattern) creates a pattern that excludes digit characters.

Data Types: pattern | char | string

Output Arguments

collapse all

Pattern expression, returned as a pattern object.

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