extractBefore with matches string

조회 수: 6 (최근 30일)
eko supriyadi
eko supriyadi 2022년 6월 4일
댓글: Voss 2022년 6월 6일
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

채택된 답변

Voss
Voss 2022년 6월 4일
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
eko supriyadi
eko supriyadi 2022년 6월 6일
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
Voss
Voss 2022년 6월 6일
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개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by