Translate string into regular expression
s2 = regexptranslate(type, s1)
s2 = regexptranslate(type, s1) translates string s1 into a regular expression string s2 that you can then use as input into one of the MATLAB® regular expression functions such as regexp. The type input can be either one of the following strings that define the type of translation to be performed. See Regular Expressions in the MATLAB Programming Fundamentals documentation for more information.
Translate all special characters (e.g., '$', '.', '?', '[') in string s1 so that they are treated as literal characters when used in the regexp and regexprep functions. The translation inserts an escape character ('\') before each special character in s1. Return the new string in s2.
Translate all wildcard and '.' characters in string s1 so that they are treated as literal wildcards and periods when used in the regexp and regexprep functions. The translation replaces all instances of '*' with '.*', all instances of '?' with '.', and all instances of '.' with '\.'. Return the new string in s2.
Because regexp interprets the sequence '\n' as a newline character, it cannot locate the two consecutive characters '\' and 'n' in this string:
str = 'The sequence \n generates a new line'; pat = '\n'; regexp(str, pat) ans = 
To have regexp interpret the expression expr as the characters '\' and 'n', first translate the expression using regexptranslate:
pat2 = regexptranslate('escape', pat) pat2 = \\n regexp(str, pat2) ans = 14
Replace the word 'walk' with 'ascend' in this string, treating the characters '$1' as a token designator:
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.
Make another replacement on the same string, this time treating the '$1' as literal characters:
regexprep(str, pat, regexptranslate('escape', 'ascend$1')) ans = I ascend$1, they ascend$1, we are ascend$1.
Given the following string of filenames, pick out just the MAT-files. Use regexptranslate to interpret the '*' wildcard as '\w+' instead of as a regular expression quantifier:
files = ['test1.mat, myfile.mat, newfile.txt, ' ... 'jan30.mat, table3.xls']; regexp(str, regexptranslate('wildcard', '*.mat'), 'match') ans = 'test1.mat' 'myfile.mat' 'jan30.mat'
To see the translation, you can type
regexptranslate('wildcard','*.mat') ans = \w+\.mat