MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

When calling any of the first three functions, pass the text to be parsed and the regular
expression in the first two input arguments. When calling regexprep, pass an additional
input that is an expression that specifies a pattern for the replacement.


Steps for Building Expressions


There are three steps involved in using regular expressions to search text for a particular
term:


1 Identify unique patterns in the string on page 2-44


This entails breaking up the text you want to search for into groups of like character
types. These character types could be a series of lowercase letters, a dollar sign
followed by three numbers and then a decimal point, etc.

2 Express each pattern as a regular expression on page 2-44


Use the metacharacters and operators described in this documentation to express
each segment of your search pattern as a regular expression. Then combine these
expression segments into the single expression to use in the search.

3 —Call the appropriate search function on page 2-45


Pass the text you want to parse to one of the search functions, such as regexp or
regexpi, or to the text replacement function, regexprep.

The example shown in this section searches a record containing contact information
belonging to a group of five friends. This information includes each person's name,
telephone number, place of residence, and email address. The goal is to extract specific
information from the text..


contacts = { ...
'Harry 287-625-7315 Columbus, OH [email protected]'; ...
'Janice 529-882-1759 Fresno, CA [email protected]'; ...
'Mike 793-136-0975 Richmond, VA [email protected]'; ...
'Nadine 648-427-9947 Tampa, FL [email protected]'; ...
'Jason 697-336-7728 Montrose, CO [email protected]'};


The first part of the example builds a regular expression that represents the format of a
standard email address. Using that expression, the example then searches the
information for the email address of one of the group of friends. Contact information for
Janice is in row 2 of the contacts cell array:


contacts{2}


Regular Expressions
Free download pdf