Main Content

maskedPattern

Pattern with specified display name

Since R2020b

Description

example

newpat = maskedPattern(pat) creates a pattern that uses the input name of pat when displaying the pattern expression of newpat. You can use maskedPattern to simplify long, complicated pattern expressions by hiding some of the details in the expression.

example

newpat = maskedPattern(pat,mask) specifies a name to display, mask, when displaying the pattern expression of newpat.

Examples

collapse all

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"

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"

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

  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

collapse all

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

collapse all

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