regexp: either 'once' or 'all'
10 ビュー (過去 30 日間)
古いコメントを表示
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 件)
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
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
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
2013 年 11 月 25 日
There's an old saying on how to write and speak: "Never say "Blah, blah, blah" when "Blah" will do. ;-)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!