extractBefore with matches string

Dear all,
i'm stuck use extractBefore with this situation:
a={'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625'}
a = 1×1 cell array
{'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625'}
matches333= extractBefore(a,'333')
matches333 = 1×1 cell array
{'32563 '}
This is because 33308 contains 333.. My goal is to retrieve the strings before pure 333 only. The result what i want is:
matches333 =
1×1 cell array
{'32563 33308 10314 20227 30113 40117 52008 81531 '}
Any idea to solve it! Tks

 采纳的回答

In this case, you can include spaces around '333' to match the pure one:
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625'}
a = 1×1 cell array
{'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625'}
matches333 = extractBefore(a,' 333 ')
matches333 = 1×1 cell array
{'32563 33308 10314 20227 30113 40117 52008 81531'}
But that won't work if '333' is at the end:
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333'}
a = 1×1 cell array
{'32563 33308 10314 20227 30113 40117 52008 81531 333'}
matches333 = extractBefore(a,' 333 ')
matches333 = 1×1 cell array
{0×0 char}

10 个评论

Here's a way that will work in both cases ('333' at the end and '333' not at the end) for this scalar cell array a:
% a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625'};
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333'};
idx = regexp(a{1},'\<333\>','once');
if isempty(idx)
matches333 = {''};
else
matches333 = {a{1}(1:idx-1)};
end
matches333
matches333 = 1×1 cell array
{'32563 33308 10314 20227 30113 40117 52008 81531 '}
how about if we have 2 cell or more, like this:
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
my solution:
matches333={};
for i=1:length(a)
idx = regexp(a{i},'\<333\>','once');
if isempty(idx)
matches333= {''};
else
matches333{i} = {a{i}(1:idx-1)}; %produce 1*2 cell
end
end
the result is column oriented, not use :
matches333=matchess33'
to solve problem, i'm interest with the looping, can you fix the loop above so the result have 2*1 cell not 1*2 cell..tks
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
% pre-allocate a column cell array
% containing empty char vectors
matches333 = repmat({''},numel(a),1);
% you can do regexp on a cell array of chars
idx = regexp(a,'\<333\>','once')
idx = 1×2 cell array
{[49]} {[49]}
for ii = 1:numel(a)
% now you only have to set matches333{ii} if there was a match
% (it's already the empty char vector otherwise)
if ~isempty(idx{ii})
matches333{ii} = a{ii}(1:idx{ii}-1);
end
end
disp(matches333);
{'32563 33308 10314 20227 30113 40117 52008 81531 '} {'32563 33308 10314 20227 30113 40117 52008 81531 '}
yes, thank you Voos, i just forgot change the matches333 matrix preallocate.. many thanks for your help :)
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
matches333 = repmat({''},numel(a),1);
for i=1:length(a)
idx = regexp(a{i},'\<333\>','once');
if isempty(idx)
matches333= {''};
else
matches333{i} = {a{i}(1:idx-1)}
end
end
edited: in early replay i'm use example
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
can, you edited your example in 3rd posting
Isn't that the same a that's already there?
no, here:
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
% copied from my comment:
old_a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
% copied from your comment:
new_a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
isequal(old_a,new_a)
ans = logical
1
Regardless of what a you want to use, the code is the same:
a = new_a;
matches333 = repmat({''},numel(a),1);
idx = regexp(a,'\<333\>','once');
for ii = 1:numel(a)
if ~isempty(idx{ii})
matches333{ii} = a{ii}(1:idx{ii}-1);
end
end
disp(matches333);
{'32563 33308 10314 20227 30113 40117 52008 81531 '} {'32563 33308 10314 20227 30113 40117 52008 81531 '}
Hi Voos how about if i want take take after number 333?
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
Therefore i make this code, would you check this looping to be more clear and intuitive (especially for regexp part), actually i'm newbie with regexp :)
after333=repmat({''},length(a),1);
for i=1:length(a)
idx = regexp(a{i},'(?<=333\s+)(\d+)','once');
after333{i} = a{i}(idx:end);
end
disp(after333)
{'56000 81625'} {1×0 char }
tks
If you want to take the remainder of each char array after '333' (where '333' is its own "word"), I would use the same regular expression as before, and adjust the indexing:
a = {'32563 33308 10314 20227 30113 40117 52008 81531 333 56000 81625','32563 33308 10314 20227 30113 40117 52008 81531 333'};
after333 = repmat({''},length(a),1);
idx = regexp(a,'\<333\>','once');
for ii = 1:numel(a)
if ~isempty(idx{ii})
after333{ii} = a{ii}(idx{ii}+4:end);
end
end
disp(after333);
{'56000 81625'} {1×0 char }

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by