Borrar filtros
Borrar filtros

regex replace whole word

12 visualizaciones (últimos 30 días)
Bardo
Bardo el 1 de Jun. de 2018
Comentada: Stephen23 el 1 de Jun. de 2018
Hi,
How to change
'v1 = v1a;'
to
'v(1) = v1a;'
using regex or regexrep?
regexprep('v1 = v1a','\bv1\b','v(1)')
apparently does not work.

Respuesta aceptada

Stephen23
Stephen23 el 1 de Jun. de 2018
Editada: Stephen23 el 1 de Jun. de 2018
>> regexprep('v1 = v1a','^([a-z]+)(\d+)','$1($2)')
ans = v(1) = v1a
Hopefully you are not constructing arbitrary strings of code for evaluating!
  8 comentarios
Bardo
Bardo el 1 de Jun. de 2018
I reckon the main requested feature - to replace only whole words matching the string - is well in the title but got lost in the discussion.
Stephen23
Stephen23 el 1 de Jun. de 2018
@Bardo: you should accept your answer, if it resolves your original question.

Iniciar sesión para comentar.

Más respuestas (1)

Bardo
Bardo el 1 de Jun. de 2018
Here we go:
>> regexprep('v1 = v1a','\<v1\>','v(1)')
ans =
v(1) = v1a
or as a strrep for whole words
function snew = subsname(s, old, new)
snew = regexprep(s,['\<', old,'\>'], new);

Categorías

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

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by