Matches as few characters of any type
creates a pattern that
matches as few of any characters as needed, including zero characters.
pat = wildcardPattern
wildcardPattern is lazy, meaning that if used on
its own without any inputs it matches empty strings (
"") before or after
characters in strings.
wildcardPattern is lazy, which means it matches as few characters of any kind as needed, even zero.
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 "" "" "" "" ""
ans = 5×1 0 0 0 0 0
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"
wildcardPattern to match individual characters of all types.
Read the text from Shakespeare's Sonnets with the
fileread function and convert it to
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
categorical so that it is a valid input for
histogram. Display the occurrences of each character using
pat = wildcardPattern(1); characters = extract(sonnets,pat); characters = lower(characters); characters = categorical(characters); histogram(characters)
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
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"
N— Exact number of characters to match
Number of characters to match, specified as a nonnegative integer scalar.
minCharacters— Minimum number of characters to match
Minimum number of characters to match, specified as a nonnegative integer scalar.
maxCharacters— Maximum number of characters to match
Maximum number of characters to match, specified as a nonnegative integer scalar.
exceptPat— Exception to wildcard matching
Exception to wildcard matching, specified as a pattern object, character vector or
wildcardPattern will match any character except
matches to this specified exception.
pat = wildcardPattern("except",digitsPattern) creates a
pattern that excludes digit characters.