maskedPattern
Pattern with specified display name
Description
Examples
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
Show all details
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 = 2×1 string
"1 + 1"
"1 + 1 = 2"
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
Show all details
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"
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
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
Show all details
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)
Show all details
Use this pattern to extract hexadecimal numbers from a string.
hexNumbers = extract("The answer is 0x2A", hexNumberPattern)
hexNumbers = "0x2A"
Input Arguments
Input pattern, specified as a pattern
, string array,
character vector, or cell array of character vectors.
Data Types: char
| string
| pattern
| cell
Masked pattern name, specified as a string scalar, character vector, or cell array of character vectors.
Data Types: char
| string
| cell
Output Arguments
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)