Linux(/
)和Windows(\
)中的路径分隔符不同.
\
字符是一个特殊的正则表达式元字符,用于形成"正则表达式转义",如\d
以匹配数字等.要匹配文字反斜杠,必须将其加倍,或escaped.
要转义任何特殊的正则表达式元字符,可以使用regexptranslate(op, str)
并将op
设置为escape
:
seperator = sprintf('%stest%s',regexptranslate('escape',filesep), regexptranslate('escape',filesep))
其他op
个可能值包括:
Type of Translation |
Description |
'escape' |
Translate all special characters in str, such as '$' , '.' , '?' ,'[' , so that they are treated as literal characters when used in regexp , regexpi , and regexprep . The translation inserts a backslash, or escape, character, '\' , before each special character in str . |
'wildcard' |
Translate all wildcard and '.' characters in str so that they are treated as literal wildcard characters and periods when used in regexp , regexpi , and regexprep . The translation replaces all instances of '*' with '.*' , all instances of '?' with '.' , and all instances of '.' with '\.' . |
'flexible' |
Replace text in str with 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) . The expression input can be a character vector or string scalar.
This syntax is equivalent to newStr = regexprep(str,expression,regexptranslate('escape',expression)) . |