How to replace leading zeroes by spaces with regexprep

조회 수: 2 (최근 30일)
Blue
Blue 2020년 7월 8일
댓글: madhan ravi 2020년 7월 9일
Hi,
I have a basic question. I simply need to replace the leading zeroes by empty spaces in table T except for the last zero which needs to remain zero. The desired output is output. I know I should probably use regexprep but the exact synthax always throws me off. Its for exportation purposes.
a = {'1231', '0002', '0103', '0000'}';
b = {'000', '000', '000', '000'}';
c = {'0', '0', '0', '0'}';
T = table(a,b,c);
d = {'1231', ' 2', ' 103', ' 0'}';
e = {' 0', ' 0', ' 0', ' 0'}';
f = {'0', '0', '0', '0'}';
output = table(d,e,f);
Thank you,

채택된 답변

Stephen23
Stephen23 2020년 7월 9일
편집: Stephen23 2020년 7월 9일
>> fun = @(c)regexprep(c,'^0+(?=\d)','${char(double($&)-16)}');
>> out = varfun(fun,T)
out =
Fun_a Fun_b Fun_c
______ _____ _____
'1231' ' 0' '0'
' 2' ' 0' '0'
' 103' ' 0' '0'
' 0' ' 0' '0'
Or
>> fun = @(c)regexprep(c,'^0+(?=\d)','${repmat('' '',1,numel($&))}');
>> out = varfun(fun,T)
out =
Fun_a Fun_b Fun_c
______ _____ _____
'1231' ' 0' '0'
' 2' ' 0' '0'
' 103' ' 0' '0'
' 0' ' 0' '0'
  댓글 수: 4
Stephen23
Stephen23 2020년 7월 9일
편집: Stephen23 2020년 7월 9일
@madhan ravi: thank you.
"Stephen it would be really great if you could make a separate thread just for regular expression. Like the other threads you made, no pressures indeed!"
So far I have only written the "dynamic variable names tutorial", which mostly consisted of collecting together hundreds of links to related discussions and adding an introduction. Time consuming certainly, but not particularly difficult.
Your suggestion is interesting. How should the tutorial be arranged? What topics need to be covered or explained that are not already covered in the documentation? Perhaps there is already an online tutorial which describes regular expressions similar to MATLAB's, it might be easier to just link to that (if we could identifiy a well-written tutorial).
madhan ravi
madhan ravi 2020년 7월 9일
”How should the tutorial be arranged? What topics need to be covered or explained that are not already covered in the documentation?”
To be honest I don’t know how it could be arranged Stephen. But it could cover for example lots of examples which are not covered in the documentation. I will add up some additional ideas in your new thread as well because at the moment the things I learnt about Regex have slightly faded away.
“Perhaps there is already an online tutorial which describes regular expressions similar to MATLAB's, it might be easier to just link to that (if we could identifiy a well-written tutorial).“
There might be many but it wouldn’t match like you describe the concepts!

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

추가 답변 (2개)

James Tursa
James Tursa 2020년 7월 8일
편집: James Tursa 2020년 7월 8일
One way:
fun = @(x)sprintf(['%' num2str(numel(x)) 'd'],str2double(x));
d = cellfun(fun,a,'uni',false);
e = cellfun(fun,b,'uni',false);
f = cellfun(fun,c,'uni',false);
  댓글 수: 1
Blue
Blue 2020년 7월 9일
Hi. Thank you for your input but this is going to be quite slow. Thats also why I dont want to convert everything to double and then use the pad function.

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


Raj Kumar Bhagat
Raj Kumar Bhagat 2020년 7월 9일
편집: Raj Kumar Bhagat 2020년 7월 9일
I tried using the basic loop statements. I was able to get the required output. Check if this code helps.
if true
p = eliminatePreleadingzeros(a);
function output = eliminatePreleadingzeros(a)
for i = 1: length(a)
tempoutput = [];
for j =1: length(char(a(i)))
tempchar = char(a(i))
if ~strcmp(tempchar(j),'0')
for k = j:length(tempchar)
tempoutput = [tempoutput, tempchar(k)];
end
break;
end
end
if isempty(tempoutput)
output(i)={'0'};
else
output(i) = {tempoutput};
end
end
output = output';
end
end

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by