Documentation

regexptranslate

Translate text into regular expression

Syntax

newStr = regexptranslate(op,str)

Description

example

newStr = regexptranslate(OP,str)翻译str变成正则表达式并返回结果newStr。You can usenewStras a regular expression in theregexp,regexpi, 和regexprepfunctions. The input argumentOP指定翻译类型regexptranslate施行。例如,如果指定OPas'逃脱', thenregexptranslate翻译特殊字符str因此它们是输出中的字面字符。newStr具有与str

Examples

collapse all

使用regexptranslatefunction. Then use the result as a regular expression inregexp

Create a character vector that contains the characters'\n'

chr ='The sequence \n generates a new line.'
chr ='序列\ n生成了一个新线路。

创建一个正则表达式'\n'as a sequence of the two consecutive characters'\''n'。自从regexpfunction interprets'\n'as a newline character, useregexptranslateto create a regular expression to escape'\n'

pattern = regexptranslate('逃脱','\n')
pattern = '\\n'

找到的起始索引'\n'inchr。阻止regexpfrom interpreting'\n'作为新线,使用patternas the regular expression.

idx = Regexp(Chr,模式)
IDX = 14

Callregexpwithout escaping'\n'。Sinceregexpinterprets'\n'as a newline, it does not find the literal characters inchr。这regexpfunction returns an empty array when it does not find a match.

idx = regexp(chr,'\n')
idx = []

Create a string. Starting in R2017a, you can create strings using double quotes instead of thestringfunction.

str ="Put your money in."
str = "Put your money in."

指定文本,'$ 0.02', as the text to replace the word'money'。To escape the'$''.'characters, useregexptranslate

r = regexptranslate('逃脱','$ 0.02')
r = '\$0\.02'

Replace'money'使用regexprepfunction.

newStr = regexprep(str,'money',r)
newStr = "Put your $0.02 in."

创建一个包含文件名的字符串数组。然后仅查找以结尾的文件名'.mat'

str = ["test1.mat","myfile.mat","my-matlab-script.m",。。。"jan30.mat","table3.xls"]
str =1x5 string arrayColumns 1 through 4 "test1.mat" "myfile.mat" "my-matlab-script.m" "jan30.mat" Column 5 "table3.xls"

要与正则表达式匹配字符串,请指定'*。垫'as the regular expression. Then translate the wildcard character,'*', using theregexptranslatefunction.

pattern = regexptranslate('wildcard','*。垫')
模式='。*\。垫'

Find matching elements instr使用由pattern

matches = regexp(str,pattern)
matches =1x5 cell array{[1]} {[1]} {0x0 double} {[1]} {0x0 double}

Create a logical array,TF, that contains1where corresponding elements ofstrmatchedpattern。这n index intostrusingTF显示以结尾的文件名'.mat'

tf = ~cellfun('isempty',matches); newStr = str(tf)
newStr =1x3 string array"test1.mat" "myfile.mat" "jan30.mat"

创建一个字符向量,其中包含由空格字符(例如空格和新线字符)分开的单词。

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'作为与空格字符匹配的正则表达式。然后更换这些字符chr

expression ='\ s'; newChr = regexptranslate('flexible',chr,expression)
newchr ='谁的\ swoods \ sthese \ sare \ si \ sthink \ si \ sknow。

Input Arguments

collapse all

翻译类型,指定为字符向量或字符串标量。您可以使用表中的选项来翻译特殊字符或通配符字符,或使用匹配的正则表达式替换文本。

Type of Translation

Description

'逃脱'

Translate all special characters instr, such as'$','.','?','[', so that they are treated as literal characters when used inregexp,regexpi, 和regexprep。这translation inserts a backslash, or escape, character,'\', before each special character instr

'wildcard'

翻译所有通配符和'.'字符中str因此,使用时将它们视为字面的通配符和时期regexp,regexpi, 和regexprep。这translation replaces all instances of'*'with'.*', all instances of'?'with'.',以及所有实例'.'with'\。'

'flexible'

更换文本strwith a regular expression that matches the text. If you specify'flexible', then also specify a regular expression to use as a replacement:newStr = regexptranslate('flexible',str,expression)。这expression输入可以是字符向量或字符串标量。

This syntax is equivalent tonewstr = regexprep(str,表达,regexptranslate('evase',expression))

输入文本,指定为字符向量,字符向量的单元格数组或字符串数​​组。

在R2006a之前引入

这个话题有帮助吗?