Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

regexptranslate

Translate text into regular expression

Syntax

s2 = regexptranslate(type,s1)
s2 = regexptranslate('flexible',s1,r)

Description

s2 = regexptranslate(type,s1) translates s1 into a regular expression, s2, that you can then use as input into one of the MATLAB® regular expression functions such as regexp. s1 can be a character vector, a cell array of character vectors, or a string array. s2 is the same type as s1. If s1 is a cell array of character vectors or a string array, then s2 is a cell array or string array that is the same size as s1. The type input can be either one of the following options that define the type of translation to be performed. See Regular Expressions in the MATLAB Programming Fundamentals documentation for more information.

Type of Translation

Description

'escape'

Translate all special characters (e.g., '$', '.', '?', '[') in 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 regular expression in s2.

'wildcard'

Translate all wildcard and '.' characters in 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 regular expression in s2.

s2 = regexptranslate('flexible',s1,r) treats r as a regular expression and replaces each matching pattern of r in s1 with the escaped regular expression of r. The input argument, r, can be a character vector, cell array of character vectors, or a string array. This syntax is equivalent to s2 = regexprep(s1,r,regexptranslate('escape',r)).

Examples

Example 1 — Using the 'escape' Option

Because regexp interprets the sequence '\n' as a newline character, it cannot locate the two consecutive characters '\' and 'n' in this character vector:

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

Example 2 — Using 'escape' In Replacement Text

Replace the word 'walk' with 'ascend' in this character vector, 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 character vector, 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.

Example 3 — Using the 'wildcard' Option

Given the following character vector 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(files, regexptranslate('wildcard', '*.mat'), 'match')

ans = 

    'test1.mat, myfile.mat, newfile.txt, jan30.mat'

To see the translation, you can type

regexptranslate('wildcard','*.mat')

ans =

.*\.mat

Example 4 — Using the 'flexible' Option

Replace newline characters with the literal, \n. Use regexptranslate with the 'flexible' option to replace matching substrings with the regular expression that matches them.

str = ['The sequence ' newline ' generates a new line.']

str =

The sequence 
 generates a new line

str = regexptranslate('flexible',str,'\n')

str =

The sequence \n generates a new line.

Introduced in R2006a

Was this topic helpful?