
- •127 Codes are ascii). The actual characters displayed depends on the
- •String operations
- •In expression, patterns are specified using combinations of metacharacters
- •In expression, patterns are specified using combinations of metacharacters
- •In arrays s1, s2, ..., sn. Inputs can be combinations of single
- •Is the same size as c1 or c2, and contains logical 1 (true) for those
- •Input c is a cell array of strings. The function returns tf, a logical
- •Is the same size as c1 or c2, and contains logical 1 (true) for those
- •Input c is a cell array of strings. The function returns tf, a logical
- •Version of the character array s.
- •Input parameters:
- •Character set conversion
- •Is unspecified or is the empty string (''), matlab's default
- •If you apply an integer or string conversion to a numeric value that
- •Xregtable/sprintf
- •Base number conversion
- •If s is a character array, each row is interpreted as a base b string.
In expression, patterns are specified using combinations of metacharacters
and literal characters. There are a few classes of metacharacters,
partially listed below. More extensive explanation can be found in the
Regular Expressions section of the MATLAB documentation.
The following metacharacters match exactly one character from its respective
set of characters:
Metacharacter Meaning
--------------- --------------------------------
. Any character
[] Any character contained within the brackets
[^] Any character not contained within the brackets
\w A word character [a-z_A-Z0-9]
\W Not a word character [^a-z_A-Z0-9]
\d A digit [0-9]
\D Not a digit [^0-9]
\s Whitespace [ \t\r\n\f\v]
\S Not whitespace [^ \t\r\n\f\v]
The following metacharacters are used to logically group subexpressions or
to specify context for a position in the match. These metacharacters do not
match any characters in the string:
Metacharacter Meaning
--------------- --------------------------------
() Group subexpression
| Match subexpression before or after the |
^ Match expression at the start of string
$ Match expression at the end of string
\< Match expression at the start of a word
\> Match expression at the end of a word
The following metacharacters specify the number of times the previous
metacharacter or grouped subexpression may be matched:
Metacharacter Meaning
--------------- --------------------------------
* Match zero or more occurrences
+ Match one or more occurrences
? Match zero or one occurrence
{n,m} Match between n and m occurrences
Characters that are not special metacharacters are all treated literally in
a match. To match a character that is a special metacharacter, escape that
character with a '\'. For example '.' matches any character, so to match
a '.' specifically, use '\.' in your pattern.
Example:
str = 'My flowers may bloom in May';
pat = 'm\w*y';
regexpi(str, pat)
returns [1 12 25]
When one of STRING or EXPRESSION is a cell array of strings, REGEXPI matches
the string input with each element of the cell array input.
Example:
str = {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'};
pat = '\s';
regexpi(str, pat)
returns {[8]; [6 10]; [7 10]}
When both STRING and EXPRESSION are cell arrays of strings, REGEXPI matches
the elements of STRING and EXPRESSION sequentially. The number of elements
in STRING and EXPRESSION must be identical.
Example:
str = {'Madrid, Spain' 'Romeo and Juliet' 'MATLAB is great'};
pat = {'\s', '\w+', '[A-Z]'};
regexpi(str, pat)
returns {[8]; [1 7 11]; [1 2 3 4 5 6 8 9 11 12 13 14 15]}
REGEXPI supports up to six outputs. These outputs may be requested
individually or in combinations by using additional input keywords. The
order of the input keywords corresponds to the order of the results. The
input keywords and their corresponding results in the default order are:
Keyword Result
--------------- --------------------------------
'start' Row vector of starting indices of each match
'end' Row vector of ending indices of each match
'tokenExtents' Cell array of extents of tokens in each match
'match' Cell array of the text of each match
'tokens' Cell array of the text of each token in each match
'names' Structure array of each named token in each match
'split' Cell array of the text delimited by each match
Example:
str = 'regexpi helps you relax';
pat = '\w*x\w*';
m = regexpi(str, pat, 'match')
returns
m = {'regexpi', 'relax'}
Example:
str = 'regexpi helps you relax';
pat = '\s+';
s = regexpi(str, pat, 'split')
returns
m = {'regexpi', 'helps', 'you', 'relax'}
Tokens are created by parenthesized subexpressions within EXPRESSION.
Example:
str = 'six sides of a hexagon';
pat = 's(\w*)s';
t = regexpi(str, pat, 'tokens')
returns
t = {{'ide'}}
Named tokens are denoted by the pattern (?<name>...). The 'names' result
structure will have fields corresponding to the named tokens in EXPRESSION.
Example:
str = 'John Davis; Rogers, James';
pat = '(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)';
n = regexpi(str, pat, 'names')
returns
n(1).first = 'John'
n(1).last = 'Davis'
n(2).first = 'James'
n(2).last = 'Rogers'
By default, REGEXPI returns all matches. To find just the first match, use
REGEXPI(STRING,EXPRESSION,'once').
REGEXPI supports international character sets.
See also regexp, regexprep, regexptranslate, strcmpi, strfind.
Reference page in Help browser
doc regexpi
<regexprep> - Replace string using regular expression.
REGEXPREP Replace string using regular expression.
S = REGEXPREP(STRING,EXPRESSION,REPLACE) replaces all occurrences of the
regular expression, EXPRESSION, in string, STRING, with the string, REPLACE.
The new string is returned. If no matches are found REGEXPREP returns
STRING unchanged.
If STRING is a cell array of strings, REGEXPREP returns a cell array of
strings replacing each element of STRING individually.
If EXPRESSION is a cell array of strings, REGEXPREP replaces each element of
EXPRESSION sequentially.
If REPLACE is a cell array of strings, then EXPRESSION must be a cell array
of strings with the same number of elements. REGEXPREP will replace each
element of EXPRESSION sequentially with the corresponding element of
REPLACE.
By default, REGEXPREP replaces all matches and is case sensitive. Available
options are:
Option Meaning
--------------- --------------------------------
'ignorecase' Ignore the case of characters when matching EXPRESSION to
STRING.
'preservecase' Ignore case when matching (as with 'ignorecase'), but
override the case of REPLACE characters with the case of
corresponding characters in STRING when replacing.
'once' Replace only the first occurrence of EXPRESSION in STRING.
N Replace only the Nth occurrence of EXPRESSION in STRING.
Example:
str = 'My flowers may bloom in May';
pat = 'm(\w*)y';
regexprep(str, pat, 'April')
returns 'My flowers April bloom in May'
regexprep(str, pat, 'April', 'ignorecase')
returns 'April flowers April bloom in April'
regexprep(str, pat, 'April', 'preservecase')
returns 'April flowers april bloom in April'
REGEXPREP can modify REPLACE using tokens from EXPRESSION. The
metacharacters for tokens are:
Metacharacter Meaning
--------------- --------------------------------
$N Replace using the Nth token from EXPRESSION
$<name> Replace using the named token 'name' from EXPRESSION
$0 Replace with the entire match
To escape a metacharacter in REGEXPREP, precede it with a '\'.
Example:
str = 'I walk up, they walked up, we are walking up, she walks.'
pat = 'walk(\w*) up'
regexprep(str, pat, 'ascend$1')
returns 'I ascend, they ascended, we are ascending, she walks.'
REGEXPREP supports international character sets.
See also regexp, regexpi, regexptranslate, strrep.
Reference page in Help browser
doc regexprep
<strcat> - Concatenate strings.
STRCAT Concatenate strings.
COMBINEDSTR = STRCAT(S1, S2, ..., SN) horizontally concatenates strings