%% Specify Names and Descriptions for Complicated Patterns
% Create a pattern from two named patterns. Naming patterns adds context to the display of the pattern.
% 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;

% Build a pattern using the named patterns that finds a word that starts and ends with D followed by a word that starts and ends with R.

dAndRWordsPat = dWordsPat + whitespacePattern + rWordsPat

% This pattern is hard to read and does not convey much information about its purpose. Use namedPattern to designate the patterns as named patterns that display specified names and descriptions in place of the pattern expressions.

dWordsPat = namedPattern(dWordsPat,"dWords", "Words that start and end with D");
rWordsPat = namedPattern(rWordsPat,"rWords", "Words that start and end with R");
dAndRWordsPat = dWordsPat + whitespacePattern + rWordsPat

% Create a string and extract the text that matches the pattern.

txt = "Dad, look at the divided river!";
words = extract(txt,dAndRWordsPat)

% Copyright 2020 The MathWorks, Inc.