Programming and Data Types    

Regular Expressions

There are several MATLAB functions that support searching and replacing characters using regular expressions. These functions are

The following tables list the regular expression syntax supported by MATLAB. This list is not intended to be a complete reference. For more information on regular expressions and their meanings, consult a reference on regular expressions.

This section contains the following topics:

Metacharacters

Metacharacters don't match themselves but describe something else. Generally they represent groups of characters or keyboard keys without a representative character.

Expression
Usage
.
Matches any single character
[ab...]
Matches any one of the characters, (a, b, etc.), contained within the brackets
[^ab...]
Matches any character except those contained within the brackets, (a, b, etc.).
[c1-c2]
Matches any characters in the range of c1 through c2.
\f
Form feed.
\n
New line.
\r
Carriage return.
\t
Tab.
\d
A digit.
Equivalent regular expression: [0-9]
\D
A nondigit.
Equivalent regular expression: [^0-9]
\s
A whitespace character.
Equivalent regular expression: [ \f\n\r\t]
\S
A non-whitespace character.
Equivalent regular expression: [^ \f\n\r\t]
\w
A word character.
Equivalent regular expression: [a-zA-Z_0-9]
\W
A nonword character.
Equivalent regular expression: [^a-zA-Z_0-9]
\
If a character has special meaning in a regular expression, precede it with this character to match it literally.

Logical Operators

Logical operators do not match any specific characters. They are used to specify the context for matching an accompanying regular expression.

Expression
Usage
(...)
Groups regular expressions.
|
Matches either the expression preceding or following it.
^
Matches following expression only at the beginning of the string.
$
Matches preceding expression only at the end of the string.
\<chars
Matches the characters when they start a word.
chars\>
Matches the characters when they end a word.
\<word\>
Exact word match.

Quantifiers

Quantifiers are a type of logical operator. They are used to specify how many instances of the previous element they can match.

Expression
Usage
*
Matches the preceding element 0 or more times.
Equivalent regular expression: {0,}
+
Matches the preceding element 1 or more times.
Equivalent regular expression: {1,}
?
Matches the preceding element 0 times or 1 time, also minimizes.
Equivalent regular expression: {0,1}
{n,m}
Must occur at least n times but no more than m times
{n,}
Must occur at least n times.
{n}
Must match exactly n times.
Equivalent regular expression: {n,n}


  Searching and Replacing Searching with Tokens