regexptranslate

Turns text or a wildcard pattern into a regular expression.

Category: Language Fundamentals > Data Types

Syntax

newStr = regexptranslate('escape', str)

Adds backslashes before characters that have special meaning in a regular expression.

newStr = regexptranslate('wildcard', str)

Converts the wildcards * and ? and the dot of a file pattern to their regular-expression equivalents.

Description

'escape' makes a literal piece of text safe to embed in an expression: a.b*c becomes a\.b\*c. 'wildcard' turns a shell-style pattern into an expression: *.m becomes .*\.m.

Input arguments

op
'escape' or 'wildcard'.
str
Character vector or string to convert.

Output arguments

newStr
The translated expression.

Example

regexptranslate('escape', 'a.b*c')
regexptranslate('wildcard', '*.m')
Output
ans = 'a\.b\*c'
ans = '.*\.m'

See also

regexp regexprep strfind contains