regexprep appending part of the replaced string

1 次查看(过去 30 天)
Hi guys, a quick question regarding regexprep() .
I am trying to convert the following string:
string1 = '\mathrm{Prmk}_{1}'
to:
string2 = 'K_{i}'
Bur when I execute the following command I get a {1} appended to the result, which I don't want. What am I missing?
string2 = regexprep(string1,'\mathrm{Prmk}_{1}','K_{i}')
>> string2 = \K_{i}{1}
Kind regards, Anton

采纳的回答

Guillaume
Guillaume 2017-2-9
编辑:Guillaume 2017-2-9
You would be better off using strrep instead of regexprep since your match expression is not a regular expression. Note that strrep uses regexprep internally.
If you want to use regexprep with an arbitrary match string that may contain characters that have meaning in regular expressions (such as the '\' and {' in your string), you need to escape these special characters with regexptranslate:
string2 = regexprep(string1, regexptranslate('escape', '\mathrm{Prmk}_{1}'), 'K_{i}')
strrep will do that for you (and for the replace expression as well). The above replace is not an issue, but if your replace string can be anything it could also be a poblem (e.g. if it contains '$')
string2 = strrep(string1 , '\mathrm{Prmk}_{1}', 'K_{i}')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by