Main Content

regexptranslate

Translate text into regular expression

Description

newStr = regexptranslate(op,str) translates str into a regular expression and returns the result in newStr. You can use newStr as a regular expression in the regexp, regexpi, and regexprep functions. The input argument op specifies the type of translation that regexptranslate performs. For example, if you specify op as 'escape', then regexptranslate translates special characters in str so that they are literal characters in the output. newStr has the same data type as str.

example

Examples

collapse all

Translate a special character in a character vector using the regexptranslate function. Then use the result as a regular expression in regexp.

Create a character vector that contains the characters '\n'.

chr = 'The sequence \n generates a new line.'
chr = 
'The sequence \n generates a new line.'

Create a regular expression that finds '\n' as a sequence of the two consecutive characters '\' and 'n'. Since the regexp function interprets '\n' as a newline character, use regexptranslate to create a regular expression to escape '\n'.

pattern = regexptranslate('escape','\n')
pattern = 
'\\n'

Find the starting index of '\n' in chr. To prevent regexp from interpreting '\n' as a newline, use pattern as the regular expression.

idx = regexp(chr,pattern)
idx = 
14

Call regexp without escaping '\n'. Since regexp interprets '\n' as a newline, it does not find the literal characters in chr. The regexp function returns an empty array when it does not find a match.

idx = regexp(chr,'\n')
idx =

     []

Create a string.

str = "Put your money in."
str = 
"Put your money in."

Specify the text, '$0.02', as the text to replace the word 'money'. To escape the '$' and '.' characters, use regexptranslate.

r = regexptranslate('escape','$0.02')
r = 
'\$0\.02'

Replace 'money' using the regexprep function.

newStr = regexprep(str,'money',r)
newStr = 
"Put your $0.02 in."

Create a string array that contains file names. Then find only the file names that end with '.mat'.

str = ["test1.mat","myfile.mat","my-matlab-script.m", ...
       "jan30.mat","table3.xls"]
str = 1x5 string
    "test1.mat"    "myfile.mat"    "my-matlab-script.m"    "jan30.mat"    "table3.xls"

To match strings with a regular expression, specify '*.mat' as the regular expression. Then translate the wildcard character, '*', using the regexptranslate function.

pattern = regexptranslate('wildcard','*.mat')
pattern = 
'.*\.mat'

Find matching elements in str using the regular expression specified by pattern.

matches = regexp(str,pattern)
matches=1×5 cell array
    {[1]}    {[1]}    {0x0 double}    {[1]}    {0x0 double}

Create a logical array, TF, that contains 1 where corresponding elements of str matched pattern. Then index into str using TF to display the file names that end with '.mat'.

tf = ~cellfun('isempty',matches);
newStr = str(tf)
newStr = 1x3 string
    "test1.mat"    "myfile.mat"    "jan30.mat"

Create a character vector that contains words separated by whitespace characters, such as spaces and newline characters.

chr = 'Whose woods these are I think I know.';
chr = [chr newline 'His house is in the village though']
chr = 
    'Whose woods these are I think I know.
     His house is in the village though'

Specify '\s' as a regular expression that matches whitespace characters. Then replace those characters in chr.

expression = '\s';
newChr = regexptranslate('flexible',chr,expression)
newChr = 
'Whose\swoods\sthese\sare\sI\sthink\sI\sknow.\sHis\shouse\sis\sin\sthe\svillage\sthough'

Input Arguments

collapse all

Type of translation, specified as a character vector or string scalar. You can translate special characters or wildcard characters, or replace text with a matching regular expression, using the options in the table.

Type of Translation

Description

'escape'

Translate all special characters in str, such as '$', '.', '?','[', so that they are treated as literal characters when used in regexp, regexpi, and regexprep. The translation inserts a backslash, or escape, character, '\', before each special character in str.

'wildcard'

Translate all wildcard and '.' characters in str so that they are treated as literal wildcard characters and periods when used in regexp, regexpi, and regexprep. The translation replaces all instances of '*' with '.*', all instances of '?' with '.', and all instances of '.' with '\.'.

'flexible'

Replace text in str with a regular expression that matches the text. If you specify 'flexible', then also specify a regular expression to use as a replacement: newStr = regexptranslate('flexible',str,expression). The expression input can be a character vector or string scalar.

This syntax is equivalent to newStr = regexprep(str,expression,regexptranslate('escape',expression)).

Input text, specified as a character vector, a cell array of character vectors, or a string array.

Version History

Introduced before R2006a