MATLAB Answers

How to replace leading zeroes by spaces with regexprep

4 views (last 30 days)
Blue
Blue on 8 Jul 2020
Commented: madhan ravi on 9 Jul 2020
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,

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 9 Jul 2020
Edited: Stephen Cobeldick on 9 Jul 2020
>> 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 Comments

Show 1 older comment
madhan ravi
madhan ravi on 9 Jul 2020
+1, Was waiting for your answer to be honest ;). Was able to manage till repmat() and numel() but couldn’t think of using (?=\d). Was so close xD. Trying to learn Regex for days but never couldn’t seem to master it :(. 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!
Stephen Cobeldick
Stephen Cobeldick on 9 Jul 2020
@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 on 9 Jul 2020
”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!

Sign in to comment.

More Answers (2)

James Tursa
James Tursa on 8 Jul 2020
Edited: James Tursa on 8 Jul 2020
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 Comment

Blue
Blue on 9 Jul 2020
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.

Sign in to comment.


Raj Kumar Bhagat
Raj Kumar Bhagat on 9 Jul 2020
Edited: Raj Kumar Bhagat on 9 Jul 2020
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

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!