필터 지우기
필터 지우기

regexp: either 'once' or 'all'

조회 수: 14 (최근 30일)
Anthony
Anthony 2013년 11월 24일
편집: Cedric 2013년 11월 25일
Hi, I want to do a search only for the first n times. But it seems the 'options' in regexp can only do 'once' or 'all'. no intermediate choice, say '5', huh? thanks a lot!
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 24일
Give an example
Anthony
Anthony 2013년 11월 24일
for example, a sentence like A='I have a dream have a dream have a dream bla bla bla...'. this sentence might be pretty long, so i wanna control for how many times it will search to make running time not too much. regexp(A,'dream','once') cannot accomplish this presumably, i can strsplit this into words using a cell, and do the regexp only for the first 40 words. something like B=strsplit(A,' '); regexp(B(1:40),'dream'); But, here comes the second reason: more important, this sentence belongs to a 10000*1 cell, each of which includes such a sentence. and i prefer to working with the entire cell array, not using any for loop. thanks a lot

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2013년 11월 24일
(pattern){5}
In some cases what you might be after more might be
N = 5; %times to match
Rexpat = [ repmat( ['(' pattern '[).*?]', 1, N-1), ')' ];
regexp(string, Rexpat)
  댓글 수: 1
Cedric
Cedric 2013년 11월 25일
편집: Cedric 2013년 11월 25일
There should be a 3rd param. 'once' in the call to REGEXP, to avoid getting multiple matches if there are multiple times N occurrences of the initial pattern.
@Anthony: I would profile both the Rexmat pattern and the original pattern, because reducing the number of matches by increasing the complexity of the pattern doesn't always lead to an increase in performance. It's quite the opposite in fact.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2013년 11월 25일
Perhaps you want strfind():
A='I have a dream have a dream have a dream bla bla bla...';
indexes = strfind(A, 'dream') % Find all locations.
indexes = indexes(1:5); % Take first 5 only.
In the command window:
indexes =
10 23 36
  댓글 수: 2
Image Analyst
Image Analyst 2013년 11월 25일
There's an old saying on how to write and speak: "Never say "Blah, blah, blah" when "Blah" will do. ;-)
Cedric
Cedric 2013년 11월 25일
+1 for the saying-driven solution ;-)

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by