maskedPattern - Pattern with specified display name - MATLAB (original) (raw)

Pattern with specified display name

Since R2020b

Syntax

Description

[newpat](#mw%5F895e17a3-7ed9-42c6-84ce-11225a7e0070%5Fsep%5Fmw%5F2527cdca-90c8-43b1-8ea8-8fa52700322c) = maskedPattern([pat](#mw%5F895e17a3-7ed9-42c6-84ce-11225a7e0070%5Fsep%5Fmw%5F619a0af2-3091-404f-9c61-e268bcf7fbcb)) creates a pattern that uses the input name of pat when displaying the pattern expression of newpat. You can usemaskedPattern to simplify long, complicated pattern expressions by hiding some of the details in the expression.

example

[newpat](#mw%5F895e17a3-7ed9-42c6-84ce-11225a7e0070%5Fsep%5Fmw%5F2527cdca-90c8-43b1-8ea8-8fa52700322c) = maskedPattern([pat](#mw%5F895e17a3-7ed9-42c6-84ce-11225a7e0070%5Fsep%5Fmw%5F619a0af2-3091-404f-9c61-e268bcf7fbcb),[mask](#mw%5F4a453aac-3e19-4ed5-a70c-22958d9286d1)) specifies a name to display, mask, when displaying the pattern expression of newpat.

example

Examples

collapse all

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)

Make Custom Pattern Functions

Create custom pattern functions and usemaskedPattern 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. UsemaskedPattern 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)

Input Arguments

collapse all

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

collapse all

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