Documentation

This is machine translation

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

regexptranslate

Translate text into regular expression

Syntax

s2 = regexptranslate(type, s1)

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

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

Introduced in R2006a

Was this topic helpful?