regexptranslate
Translate text into regular expression
Description
newStr = regexptranslate(
translates op
,str
)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
.
Examples
Translate Special Character
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 = []
Escape Special Characters in Replacement Text
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."
Translate Wildcard Characters
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"
Replace Text with Regular Expression
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
op
— Translation type
'escape'
| 'wildcard'
| 'flexible'
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 |
---|---|
| Translate all special characters in |
| Translate all wildcard and |
| Replace text in This syntax
is equivalent to |
str
— Input text
character vector | cell array of character vectors | string array
Input text, specified as a character vector, a cell array of character vectors, or a string array.
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)