Replace regular expression in file name

조회 수: 2 (최근 30일)
g
g 2019년 1월 31일
댓글: Stephen23 2019년 1월 31일
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
Stephen23
Stephen23 2019년 1월 31일
@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?

댓글을 달려면 로그인하십시오.

답변 (2개)

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

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2019년 1월 31일
Why are you not using fullfile() instead?
S = '/home/directory1/sub';
new_S = regexprep(S, '(?<!/)$', '/', 'emptymatch');

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by