How to replace leading zeroes by spaces with regexprep
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
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
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
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);
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!