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
채택된 답변
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
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
Voss
2022년 6월 4일
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에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
