MATLAB Function Reference Previous page   Next Page
regexprep

Replace string using regular expression

Syntax

Description

s = regexprep('str', 'expr', 'repstr') replaces all occurrences of the regular expression expr in string str with the string repstr. The new string is returned in s. If no matches are found, return string s is the same as input string str. You can use character representations (e.g., '\t' for tab, or '\n' for newline) in replacement string repstr.

If str is a cell array of strings, then the regexprep return value s is always a cell array of strings having the same dimensions as str.

If expr is a cell array of strings and repstr is a single string, regexprep uses the same replacement string on each expression in expr. If both expr and repstr are cell arrays of strings, then expr and repstr must contain the same number of elements, and regexprep pairs each repstr element with its matching element in expr.

You can capture parts of the input string as tokens and then reuse them in the replacement string. Specify the parts of the string to capture using the (...) operator. Specify the tokens to use in the replacement string using the operators $1, $2, $N to reference the first, second, and Nth tokens captured. (See the section on "Tokens and the example Using Tokens in a Replacement String in the External Interfaces documentation for information on using tokens.)

s = regexprep('str', 'expr', 'repstr' optionlist) By default, regexprep replaces all matches and is case sensitive. You can use one or more of the following options with regexprep. Separate each option in optionlist with a comma.

Option
Description
'ignorecase'
Ignore the case of characters when matching expr to str.
'preservecase'
Ignore case when matching (as with 'ignorecase'), but override the case of replace characters with the case of corresponding characters in str when replacing.
'once'
Replace only the first occurrence of expr in str.
N
Replace only the Nth occurrence of expr in str.

Remarks

See Regular Expressions in the MATLAB documentation for a listing of all regular expression metacharacters supported by MATLAB.

regexprep does not support international character sets.

Examples

Example 1

Perform a case-sensitive replacement on words starting with m and ending with y:

Replace all words starting with m and ending with y, regardless of case, but maintain the original case in the replacement strings:

Example 2

Replace all variations of the words 'walk up' using the letters following walk as a token. In the replacement string

Example 3

This example operates on a cell array of strings. It searches for consecutive matching letters (e.g., 'oo') and uses a common replacement value ('--') for all matches. The function returns a cell array of strings having the same dimensions as the input cell array:

See Also

regexp, regexpi, strfind, findstr, strmatch, strcmp, strcmpi, strncmp, strncmpi


Previous page  regexp, regexpi rehash Next page

© 1994-2005 The MathWorks, Inc.