regexptranslate
Translate text into regular expression
Syntax
newStr = regexptranslate(op,str)
Description
Examples
翻译特殊字符
使用regexptranslate
function. 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'
。自从regexp
function interprets'\n'
as a newline character, useregexptranslate
to create a regular expression to escape'\n'
。
pattern = regexptranslate('逃脱','\n')
pattern = '\\n'
找到的起始索引'\n'
inchr
。阻止regexp
from interpreting'\n'
作为新线,使用pattern
as the regular expression.
idx = Regexp(Chr,模式)
IDX = 14
Callregexp
without escaping'\n'
。Sinceregexp
interprets'\n'
as a newline, it does not find the literal characters inchr
。这regexp
function 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 thestring
function.
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'
使用regexprep
function.
newStr = regexprep(str,'money',r)
newStr = "Put your $0.02 in."
Translate Wildcard Characters
创建一个包含文件名的字符串数组。然后仅查找以结尾的文件名'.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 theregexptranslate
function.
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 contains1
where corresponding elements ofstr
matchedpattern
。这n index intostr
usingTF
显示以结尾的文件名'.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
OP
—Translation type
'逃脱'
|'wildcard'
|'flexible'
翻译类型,指定为字符向量或字符串标量。您可以使用表中的选项来翻译特殊字符或通配符字符,或使用匹配的正则表达式替换文本。
Type of Translation |
Description |
---|---|
|
Translate all special characters in |
|
翻译所有通配符和 |
|
更换文本 This syntax is equivalent to |
str
—输入文本
character vector|cell array of character vectors|string array
输入文本,指定为字符向量,字符向量的单元格数组或字符串数组。
在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:。
您还可以从以下列表中选择一个网站:
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éricaLatina(Español)
- Canada(English)
- United States(English)
欧洲
- Belgium(English)
- 丹麦(English)
- Deutschland(德意志)
- España(Español)
- Finland(English)
- 法国(Français)
- 爱尔兰(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- 挪威(English)
- Österreich(德意志)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)