MATLAB Answers


specific pattern from the file name

Asked by ayman mounir on 26 Jul 2019
Latest activity Edited by ayman mounir on 28 Jul 2019
I want to extract the project number from the file name
example the file fame is: 'abcdd-22_Z12'
the project number should be Z12 for sure it is dynamic name could in the next file name Z11 for intance.
which expression I should use


Sign in to comment.

2 Answers

Answer by per isakson
on 26 Jul 2019
Edited by per isakson
on 26 Jul 2019
 Accepted Answer

These statements
chr = 'abcdd-22_Z12';
cac = regexp( chr, '(?<=_)Z\d{2}', 'match' );
ans =
This regex, '(?<=_)Z\d{2}', matches a literal "Z" followed by two digits, which is preceded by underscore.

  1 Comment

Thanks It works perfectly

Sign in to comment.

Answer by Walter Roberson
on 26 Jul 2019

[~, basename, ext] = fileparts(FileName);
parts = strsplit(basename, '_');
project = parts{end};
In some cases this can be simplified: for example if the directory and extension are already removed from FileName then
project = regexp(FileName, '(?<=_).*', 'match');

  1 Comment

Thanks It works perfect

Sign in to comment.