Effacer les filtres
Effacer les filtres

regexprep appending part of the replaced string

1 vue (au cours des 30 derniers jours)
A dB
A dB le 9 Fév 2017
Commenté : A dB le 9 Fév 2017
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

Réponse acceptée

Guillaume
Guillaume le 9 Fév 2017
Modifié(e) : Guillaume le 9 Fév 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}')

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by