フィルターのクリア

How to optimize a code to find postfix in a sting

2 ビュー (過去 30 日間)
pamela sulis
pamela sulis 2015 年 11 月 23 日
コメント済み: pamela sulis 2015 年 11 月 23 日
Hi! I have a cell 106x1 of strings 'TrajCompact'. In every string I want to find all the prefix that are combination of (0,1,2,3,4) and consider only the postfix after this prefix My prefix are: 00,01,02,03,04,10,11,12,13,14,20,21,22,23,24,30,31,32,33,34,40,41,42,43,44
I try to do this, using the code
for k=1:size(TrajCompact,1)
matches(k) = regexp(TrajCompact(k), '\(?00.*', 'match', 'once');
end
The code run but I have to write it 25 times: one for every prefix. I want to find a compact expression so I modify the code in this way
[digits{1:2}] = ndgrid(0:4);
for k=1:106
matches(k) = regexp(TrajCompact(k), sprintf('?%d%d.*', digits{1}(k), digits{2}(k)), 'match', 'once');
end
but it doesn't make what I want, can you help me to find the error Thanks
  2 件のコメント
Stephen23
Stephen23 2015 年 11 月 23 日
編集済み: Stephen23 2015 年 11 月 23 日
You should try using my FEX submission make_regexp:
This creates a figure which lets you interactively create regular expressions, and alter them while it shows you the regexp outputs. It lets you change your regular expressions and immediately see what effect it has on the string matching.
pamela sulis
pamela sulis 2015 年 11 月 23 日
I have some problems to understand the code: I'm not very expert about matlab :(

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 11 月 23 日
matches(k) = regexp(TrajCompact(k), '(?<=\(?[0-4]{2}).*', 'match', 'once');
this would strip off an optional '(' followed by a single two-digit number that is composed of the digits 0-4 only, and the rest of the string would be returned.
If this is not what you want please post a sample input line.

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 11 月 23 日
編集済み: Stephen23 2015 年 11 月 23 日
Your first regular expression has a backslash, but your sprint version does not. The first regular expression:
>> '\(?00.*'
ans = \(?00.*
The second regular expression:
>> sprintf('?%d%d.*', 0, 0)
ans = ?00.*
The strings look quite different to me. Perhaps you wanted this:
>> sprintf('\\(?%d%d.*', 0, 0)
ans = \(?00.*

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by