MATLAB Answers

Regexp: different behavior for the same type of expressions

Asked by Giorgos Papakonstantinou on 19 Sep 2013

I want to capture everything except the tokens:

name, '_' and '.iv2'

name =
 >> filename'
 ans = 

I do this:

str=regexp(filename, [ '(?:[^' name '_])\w*(?:[^.iv2])' ], 'match');

And the answer is the following!

>> celldisp(str)
str{1}{1} =
str{2}{1} =
str{3}{1} =
str{4}{1} =
str{5}{1} =
str{6}{1} =
str{7}{1} =
str{8} =

I don't understand why regexp has a different behavior for i.e. in filename(1) and filename(3)


Cedric Wannaz
on 19 Sep 2013

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.

Thank you very much Cedric! Very nice explanation!



No products are associated with this question.

2 Answers

Answer by Vishal Rane
on 19 Sep 2013
Edited by Vishal Rane
on 19 Sep 2013
 Accepted answer

You can use:

regexprep( filename, [ name, '_|.iv2'], '')


regexp( filename, [ name, '_(\w*).iv2'], 'tokens')

  1 Comment

Thank you Vishal! Much more compact syntax! However, do you know why regexp has this behavior?

Answer by Andrei Bobrov
on 19 Sep 2013
Edited by Andrei Bobrov
on 19 Sep 2013
str = {'A7122_60a.iv2'
cl = regexp(str,'(?<=_)\w*(?=\.)','match');
out = cat(1,cl{:});


Thank you Andrei! Do you know why is this happening?

Andrei Bobrov
on 20 Sep 2013

Please read about regexp, parts:

- Regular expression :


Metacharacters ( \w ),

Quantifiers ( expr* ),

Lookaround Assertions ( expr(?=test) and (?<=test)expr )

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!