| MATLAB Function Reference | ![]() |
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' By default, optionlist)
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.
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.
Example 1
Perform a case-sensitive replacement on words starting with m and ending with y:
str = 'My flowers may bloom in May'; pat = 'm(\w*)y'; regexprep(str, pat, 'April') ans = My flowers April bloom in May
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
str = 'I walk up, they walked up, we are walking up.'; pat = 'walk(\w*) up'; regexprep(str, pat, 'ascend$1') ans = I ascend, they ascended, we are ascending.
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:
str = { ... 'Whose woods these are I think I know.' ; ... 'His house is in the village though;' ; ... 'He will not see me stopping here' ; ... 'To watch his woods fill up with snow.'}; a = regexprep(str, '(.)\1', '--', 'ignorecase') a = 'Whose w--ds these are I think I know.' 'His house is in the vi--age though;' 'He wi-- not s-- me sto--ing here' 'To watch his w--ds fi-- up with snow.'
See Also
regexp, regexpi, strfind, findstr, strmatch, strcmp, strcmpi, strncmp, strncmpi
| regexp, regexpi | rehash | ![]() |
© 1994-2005 The MathWorks, Inc.