specific pattern from the file name

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

 採用された回答

per isakson
per isakson 2019 年 7 月 26 日
編集済み: per isakson 2019 年 7 月 26 日

0 投票

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

1 件のコメント

ayman mounir
ayman mounir 2019 年 7 月 27 日
編集済み: ayman mounir 2019 年 7 月 28 日
Thanks It works perfectly

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 7 月 26 日

0 投票

[~, 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 件のコメント

ayman mounir
ayman mounir 2019 年 7 月 27 日
Thanks It works perfect

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

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

質問済み:

2019 年 7 月 26 日

編集済み:

2019 年 7 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by