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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)