Programming Previous page   Next Page

Character Classes

Character classes represent either a specific set of characters (e.g., uppercase) or a certain type of character (e.g., non-white-space).

Operator
Usage
.
Any single character, including white space
[c1c2c3]
Any character contained within the brackets: c1 or c2 or c3
[^c1c2c3]
Any character not contained within the brackets: anything but c1 or c2 or c3
[c1-c2]
Any character in the range of c1 through c2
\s
Any white-space character; equivalent to [ \f\n\r\t\v]
\S
Any non-white-space character; equivalent to
[^ \f\n\r\t\v]
\w
Any alphabetic, numeric, or underscore character; equivalent to [a-zA-Z_0-9]
\W
Any character that is not alphabetic, numeric, or underscore; equivalent to [^a-zA-Z_0-9]
\d
Any numeric digit; equivalent to [0-9]
\D
Any nondigit character; equivalent to [^0-9]

These examples demonstrate how to use the character classes listed above. See the regexp reference page for help with syntax.

Most of these examples use the following string:

Any Character -- .

Use '..ain' in an expression to match a sequence of five characters ending in 'ain'. Note that . matches white-space characters as well:

Matches ' rain', 'Spain', ' main', and 'plain'.

Returning Strings Rather than Indices.   Here is the same example, this time specifying the command qualifier 'match'. In this case, regexp returns the text of the matching strings rather than the starting index:

Selected Characters -- [c1c2c3]

Use [c1c2c3] in an expression to match selected characters r, p, or m followed by 'ain'. Specify two qualifiers this time, 'match' and 'start', along with an output argument for each, mat and idx. This returns the matching strings and the starting indices of those strings:

Range of Characters -- [c1 - c2]

Use [c1-c2] in an expression to find words that begin with a letter in the range of A through Z:

Word and White-Space Characters -- \w, \s

Use \w and \s in an expression to find words that end with the letter n followed by a white-space character. Add a new qualifier, 'end', to return the str index that marks the end of each match:

Numeric Digits -- \d

Use \d to find numeric digits in the following string:


Previous page  Elements of an Expression Character Representation Next page

© 1994-2005 The MathWorks, Inc.