regexprep appending part of the replaced string
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Guillaume
am 9 Feb. 2017
Bearbeitet: Guillaume
am 9 Feb. 2017
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}')
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!