maskedPattern
Description
Examples
Hide Details When Displaying Complicated Patterns
Use maskedPattern
to display a variable in place of a complicated pattern expression.
Build a pattern that matches simple arithmetic expressions composed of numbers and arithmetic operators.
mathSymbols = asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)
mathSymbols = pattern
Matching:
asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)
Build a pattern that matches arithmetic expressions with whitespaces between characters using mathSymbols
.
longExpressionPat = asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols
longExpressionPat = pattern
Matching:
asManyOfPattern(asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1) + whitespacePattern) + asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)
The displayed pattern expression is long and difficult to read. Use maskedPattern
to display the variable name, mathSymbols
, in place of the pattern expression.
mathSymbols = maskedPattern(mathSymbols); shortExpressionPat = asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols
shortExpressionPat = pattern
Matching:
asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols
Use details to show more information
Create a string containing some arithmetic expressions, and then extract the pattern from the text.
txt = "What is the answer to 1 + 1? Oh, I know! 1 + 1 = 2!";
arithmetic = extract(txt,shortExpressionPat)
arithmetic = 2x1 string
"1 + 1"
"1 + 1 = 2"
Display Specified Pattern Names
Use maskedPattern
to display a specified name for a complicated pattern expression.
Build two patterns: one that matches words that begin and end with the letter D, and one that matches words that begin and end with the letter R.
dWordsPat = letterBoundary + caseInsensitivePattern("d" + lettersPattern + "d") + letterBoundary; rWordsPat = letterBoundary + caseInsensitivePattern("r" + lettersPattern + "r") + letterBoundary;
Use maskedPattern
to display a specified name in place of the pattern expressions. Build a pattern using the masked patterns that find words that start and end with D followed by a word that starts and ends with R.
dWordsPat = maskedPattern(dWordsPat,"Words that start and end with D"); rWordsPat = maskedPattern(rWordsPat,"Words that start and end with R"); dAndRWordsPat = dWordsPat + whitespacePattern + rWordsPat
dAndRWordsPat = pattern
Matching:
Words that start and end with D + whitespacePattern + Words that start and end with R
Use details to show more information
Create a string, and then extract the pattern from the text.
txt = "Dad, look at the divided river!";
words = extract(txt,dAndRWordsPat)
words = "divided river"
Make Custom Pattern Functions
Create custom pattern functions and use
maskedPattern
to hide details.
Create a function atLeastOneOfPattern
that takes the input
pattern pat
and creates a pattern, newPat
, that
matches one or more consecutive instances of pat
. Use
maskedPattern
to hide the details of the pattern when
displayed.
function newPat = atLeastOneOfPattern(pat) arguments pat pattern end newPat = asManyOfPattern(pat,1); newPat = maskedPattern(newPat,compose("atLeastOneOfPattern(%s)",pat)); end
Call atLeastOneOfPattern
with the input "a"
and display newPat
.
newPat = atLeastOneOfPattern("a")
newPat = pattern Matching: atLeastOneOfPattern("a") Show all details
Match Hexadecimal Numbers
Create a pattern hexDigit
that matches numeric characters from 0-9 and letters from a-f with characterListPattern
. Since characterListPattern
is case sensitive, use caseInsensitivePattern
so that hexDigit
matches regardless of character case.
hexDigit = characterListPattern('0','9') | characterListPattern('a','f'); hexDigit = caseInsensitivePattern(hexDigit); hexDigit = maskedPattern(hexDigit)
hexDigit = pattern
Matching:
hexDigit
Use details to show more information
hexDigit
matches individual digits of hexadecimal numbers. Match full hexadecimal numbers by building a pattern that matches the literal text "0x"
followed by one or more occurrences of hexDigit
.
hexNumberPattern = "0x" + asManyOfPattern(hexDigit, 1)
hexNumberPattern = pattern
Matching:
"0x" + asManyOfPattern(hexDigit,1)
Use details to show more information
Use this pattern to extract hexadecimal numbers from a string.
hexNumbers = extract("The answer is 0x2A", hexNumberPattern)
hexNumbers = "0x2A"
Input Arguments
pat
— Input pattern
pattern array | string array | character vector | cell array of character vectors
Input pattern, specified as a pattern
, string array,
character vector, or cell array of character vectors.
Data Types: char
| string
| pattern
| cell
mask
— Masked pattern name
string array | character vector | cell array of character vectors
Masked pattern name, specified as a string scalar, character vector, or cell array of character vectors.
Data Types: char
| string
| cell
Output Arguments
newpat
— Output pattern
pattern (default) | array of pattern objects
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)