regexprep

Returns the result after replacing the regular expression, pat with rep in the input, src.

Syntax

R = regexprep(src, pat, rep)

R = regexprep(src, pat, rep, options...)

Inputs

src
Type: string | cell
pattern
Type: string | cell
rep
Type: string | cell
options (optional)
Arguments specifying the search/replace pattern. Valid options are:
once
Replaces only the first occurrence of the matching pattern.
ignorecase
Ignores the case of the matching pattern.
emptymatch
Matches empty sequences.
Type: string

Outputs

R
Type: string

Examples

Replacement with default options:
R = regexprep ('www123.com', '(1)', '.$1')
R = www.123.com
Case-insensitive replacement:
R = regexprep ('1(a)3[A]5-6{7}', '[^A-Z0-9_]', ':', 'ignorecase')
R = 1:a:3:A:5:6:7:
Case-insensitive, single replacement:
R = regexprep ('1(a)3[A]5-6{7}', '[^A-Z0-9_]', ':', 'ignorecase', 'once')
1:a)3[A]5-6{7}
Replacement with cell array inputs:
R = regexprep ({'123', '312'}, {'3', '2'}, {'xy', 'abc'})
R =
{
[1,1] 1abcxy
[2,1] xy1abc
}