Borrar filtros
Borrar filtros

regexprep appending part of the replaced string

1 visualización (últimos 30 días)
A dB
A dB el 9 de Feb. de 2017
Comentada: A dB el 9 de Feb. de 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

Respuesta aceptada

Guillaume
Guillaume el 9 de Feb. de 2017
Editada: Guillaume el 9 de Feb. de 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}')

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by