Azzera filtri
Azzera filtri

regexprep appending part of the replaced string

1 visualizzazione (ultimi 30 giorni)
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

Risposta accettata

Guillaume
Guillaume il 9 Feb 2017
Modificato: Guillaume il 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}')

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by