%% Find Letters and Words Using Patterns
% Since R2020b
% Create a character vector.

str = 'Find the letters.'

%
% Create a pattern that matches sequences of letters using the lettersPattern function.

pat = lettersPattern

%
% Find the index of each letter. While pat matches a sequence of letters having any length, strfind stops as soon as it finds a match and then proceeds to the next match. For example, 'Find' and 'F' are both matches for lettersPattern, since the number of letters for a match is not specified. But strfind matches 'F' first and returns its index. Then strfind matches 'i', and so on. (You can call lettersPattern with an optional argument that specifies the number of letters to match.)

k = strfind(str,pat)

%
% To find the starts of words, call lettersPattern with boundaries. The letterBoundary function matches a boundary between letters and nonletter characters.

pat = letterBoundary + lettersPattern
k = strfind(str,pat)

%
% For a list of functions that create pattern objects, see pattern.
% Copyright 2020 The MathWorks, Inc.