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 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 = []
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."
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 = 1×5 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]} {0×0 double} {[1]} {0×0 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 = 1×3 string
"test1.mat" "myfile.mat" "jan30.mat"
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
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 |
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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)