MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Regular Expressions


In this section...
“What Is a Regular Expression?” on page 2-41
“Steps for Building Expressions” on page 2-43
“Operators and Characters” on page 2-46

What Is a Regular Expression?


A regular expression is a sequence of characters that defines a certain pattern. You
normally use a regular expression to search text for a group of words that matches the
pattern, for example, while parsing program input or while processing a block of text.

The character vector 'Joh?n\w*' is an example of a regular expression. It defines a
pattern that starts with the letters Jo, is optionally followed by the letter h (indicated by
'h?'), is then followed by the letter n, and ends with any number of word characters,
that is, characters that are alphabetic, numeric, or underscore (indicated by '\w*'). This
pattern matches any of the following:

Jon, John, Jonathan, Johnny

Regular expressions provide a unique way to search a volume of text for a particular
subset of characters within that text. Instead of looking for an exact character match as
you would do with a function like strfind, regular expressions give you the ability to
look for a particular pattern of characters.

For example, several ways of expressing a metric rate of speed are:

km/h
km/hr
km/hour
kilometers/hour
kilometers per hour

You could locate any of the above terms in your text by issuing five separate search
commands:

strfind(text, 'km/h');
strfind(text, 'km/hour');
% etc.

Regular Expressions
Free download pdf