%% Create Named Patterns to Organize Complicated Patterns
% Use namedPattern to assign a pattern to a named pattern.
% Build a pattern that matches simple arithmetic expressions composed of numbers and arithmetic operators.

mathSymbols = asManyOfPattern(digitsPattern | characterListPattern("+-*/="),1)

% Build a pattern that matches arithmetic expressions with whitespaces between characters using the named pattern.

longExpressionPat = asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols

% This expression is long and hard to read. Use namedPattern to assign the pattern to the named pattern, mathSymbols.

mathSymbols = namedPattern(mathSymbols);
shortExpressionPat = asManyOfPattern(mathSymbols + whitespacePattern) + mathSymbols

% 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)

% Copyright 2020 The MathWorks, Inc.