Borrar filtros
Borrar filtros

Replace regular expression in file name

1 visualización (últimos 30 días)
g
g el 31 de En. de 2019
Comentada: Stephen23 el 31 de En. de 2019
I want to use regular expressions replacement for file names in the following way.
Say I have a string '/home/directory1/sub'
I want to replace this with the string 'home/directory1/sub/' (so this needs to work for combinations of letters, numbers, characters)
or if it already had the / at the end, to leave it as is.
How would I do this in this way? I've tried using the documentation but can't seem to get it right. (Also, I can only use pre2015 commands)
  1 comentario
Stephen23
Stephen23 el 31 de En. de 2019
@grant: just out of curiosity, what is the user case for this? Have you tried using fullfile, which avoids the need to mess around with file separators?

Iniciar sesión para comentar.

Respuestas (2)

madhan ravi
madhan ravi el 31 de En. de 2019
str='/home/directory1/subs'
if strcmp(str(end),'/')
return
else
expp='$'
str=regexprep(str,expp,'/','emptymatch')
end
  2 comentarios
Walter Roberson
Walter Roberson el 31 de En. de 2019
If you already tested str(end) for / then you might as well just append,
str = [str '/'];
madhan ravi
madhan ravi el 31 de En. de 2019
True sir Walter but wanted to indulge with regexprep :-)

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 31 de En. de 2019
Why are you not using fullfile() instead?
S = '/home/directory1/sub';
new_S = regexprep(S, '(?<!/)$', '/', 'emptymatch');

Categorías

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

Etiquetas

Productos


Versión

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by