Regexp: different behavior for the same type of expressions

3 ビュー (過去 30 日間)
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 9 月 19 日
I want to capture everything except the tokens:
name, '_' and '.iv2'
name =
A7122
>> filename'
ans =
'A7122_60a.iv2'
'A7122_60b.iv2'
'A7122_70a.iv2'
'A7122_70b.iv2'
'A7122_90a.iv2'
'A7122_90b.iv2'
'A7122_100.iv2'
'A7122_120.iv2'
I do this:
str=regexp(filename, [ '(?:[^' name '_])\w*(?:[^.iv2])' ], 'match');
And the answer is the following!
>> celldisp(str)
str{1}{1} =
60a
str{2}{1} =
60b
str{3}{1} =
0a
str{4}{1} =
0b
str{5}{1} =
90a
str{6}{1} =
90b
str{7}{1} =
00
str{8} =
{}
I don't understand why regexp has a different behavior for i.e. in filename(1) and filename(3)
  2 件のコメント
Cedric
Cedric 2013 年 9 月 19 日
編集済み: Cedric 2013 年 9 月 20 日
Your mistake is that [^A7122_] doesn't stand for "any six letters expression that is not 'A7122 _'", but instead for "any character which is not in the pool of literals {'A', '7', '1', '2', '_' }". The same applies to [^.iv2]. This is why 70a and 70b for example are not matched, but you get instead 0a and 0b.
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 9 月 20 日
Thank you very much Cedric! Very nice explanation!

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

採用された回答

Vishal Rane
Vishal Rane 2013 年 9 月 19 日
編集済み: Vishal Rane 2013 年 9 月 19 日
You can use:
regexprep( filename, [ name, '_|.iv2'], '')
Also
regexp( filename, [ name, '_(\w*).iv2'], 'tokens')
  1 件のコメント
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 9 月 19 日
Thank you Vishal! Much more compact syntax! However, do you know why regexp has this behavior?

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 9 月 19 日
編集済み: Andrei Bobrov 2013 年 9 月 19 日
str = {'A7122_60a.iv2'
'A7122_60b.iv2'
'A7122_70a.iv2'
'A7122_70b.iv2'
'A7122_90a.iv2'
'A7122_90b.iv2'
'A7122_100.iv2'
'A7122_120.iv2'}
cl = regexp(str,'(?<=_)\w*(?=\.)','match');
out = cat(1,cl{:});
  2 件のコメント
Giorgos Papakonstantinou
Giorgos Papakonstantinou 2013 年 9 月 19 日
Thank you Andrei! Do you know why is this happening?
Andrei Bobrov
Andrei Bobrov 2013 年 9 月 20 日
Please read about regexp, parts:
- Regular expression :
about
Metacharacters ( \w ),
Quantifiers ( expr* ),
Lookaround Assertions ( expr(?=test) and (?<=test)expr )

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

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by