How to replace leading zeroes by spaces with regexprep

5 vues (au cours des 30 derniers jours)
Blue
Blue le 8 Juil 2020
Commenté : madhan ravi le 9 Juil 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,

Réponse acceptée

Stephen23
Stephen23 le 9 Juil 2020
Modifié(e) : Stephen23 le 9 Juil 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 commentaires
Stephen23
Stephen23 le 9 Juil 2020
Modifié(e) : Stephen23 le 9 Juil 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 le 9 Juil 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!

Connectez-vous pour commenter.

Plus de réponses (2)

James Tursa
James Tursa le 8 Juil 2020
Modifié(e) : James Tursa le 8 Juil 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 commentaire
Blue
Blue le 9 Juil 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.

Connectez-vous pour commenter.


Raj Kumar Bhagat
Raj Kumar Bhagat le 9 Juil 2020
Modifié(e) : Raj Kumar Bhagat le 9 Juil 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

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by