regexprep - Replace string using regular expression

Syntax

s = regexprep('str', 'expr', 'repstr')
s = regexprep('str', 'expr', 'repstr' options)

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. See Regular Expressions in the MATLAB® Programming Fundamentals documentation for more information.

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.

To specify more than one expression to match or more than one replacement string, see the guidelines listed below under Multiple Expressions or Replacement Strings.

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 Tokens and the example Using Tokens in a Replacement String in the MATLAB Programming Fundamentals documentation for information on using tokens.)

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

Option

Description

mode

See mode descriptions on the regexp reference page.

N

Replace only the Nth occurrence of expr in str.

'once'

Replace only the first occurrence of expr in str.

'ignorecase'

Ignore case when matching and when replacing.

'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.

'warnings'

Display any hidden warning messages issued by MATLAB during the execution of the command. This option only enables warnings for the one command being executed.

Remarks

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

Multiple Expressions or Replacement Strings

In the case of multiple expressions and/or replacement strings, regexprep attempts to make all matches and replacements. The first match is against the initial input string. Successive matches are against the string resulting from the previous replacement.

The expr and repstr inputs follow these rules:

Examples

Example 1 — Making a Case-Sensitive Replacement

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:

regexprep(str, pat, 'April', 'preservecase')
ans =
   April flowers april bloom in April

Example 2 — Using Tokens In the Replacement String

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 — Operating on Multiple Strings

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, regexptranslate, strfind, findstr, strmatch, strcmp, strcmpi, strncmp, strncmpi

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS