Programming |
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).
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 [c
1c
2c
3]
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 [c
1-c
2]
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:
[mat ix1 ix2] = regexp(str, '\w*n\s', 'match', 'start', 'end') mat = 'rain ' 'in ' 'Spain ' 'on ' ix1 = 5 10 13 32 ix2 = 9 12 18 34
Numeric Digits -- \d
Use \d
to find numeric digits in the following string:
numstr = 'Easy as 1, 2, 3'; [mat idx] = regexp(numstr, '\d', 'match', 'start') mat = '1' '2' '3' idx = 9 12 15
Elements of an Expression | Character Representation |
© 1994-2005 The MathWorks, Inc.