%% Search and Replace Text
% Since R2016b. Replaces Searching and Replacing (R2016a).
% Processing text data often involves finding and replacing substrings. There are several functions that find text and return different information: some functions confirm that the text exists, while others count occurrences, find starting indices, or extract substrings. These functions work on character vectors and string scalars, such as "yes", as well as character and string arrays, such as ["yes","no";"abc","xyz"]. In addition, you can use patterns to define rules for searching, such as one or more letter or digit characters.
%% Search for Text
% To determine if text is present, use a function that returns logical values, like contains, startsWith, or endsWith. Logical values of 1 correspond to true, and 0 corresponds to false.

txt = "she sells seashells by the seashore"; 
TF = contains(txt,"sea")

%
% Calculate how many times the text occurs using the count function.

n = count(txt,"sea")

%
% To locate where the text occurs, use the strfind function, which returns starting indices.

idx = strfind(txt,"sea")

%
% Find and extract text using extraction functions, such as extract, extractBetween, extractBefore, or extractAfter.

mid = extractBetween(txt,"sea","shore")

%
% Optionally, include the boundary text.

mid = extractBetween(txt,"sea","shore","Boundaries","inclusive")

%
%% Find Text in Arrays
% The search and replacement functions can also find text in multi-element arrays. For example, look for color names in several song titles.

songs = ["Yellow Submarine"; 
         "Penny Lane";  
         "Blackbird"]; 

colors =["Red","Yellow","Blue","Black","White"]; 

TF = contains(songs,colors)

%
% To list the songs that contain color names, use the logical TF array as indices into the original songs array. This technique is called logical indexing.

colorful = songs(TF)

%
% Use the function replace to replace text in songs that matches elements of colors with the string "Orange".

replace(songs,colors,"Orange")

%
%% Match Patterns
% Since R2020b
% In addition to searching for literal text, like “sea” or “yellow”, you can search for text that matches a pattern. There are many predefined patterns, such as digitsPattern to find numeric digits.

address = "123a Sesame Street, New York, NY 10128"; 
nums = extract(address,digitsPattern) 

%
% For additional precision in searches, you can combine patterns. For example, locate words that start with the character “S”.  Use a string to specify the “S” character, and lettersPattern to find additional letters after that character.

pat = "S" + lettersPattern; 
StartWithS = extract(address,pat) 

%
% For more information, see Build Pattern Expressions.
% Copyright 2012 The MathWorks, Inc.