specific pattern from the file name

조회 수: 3 (최근 30일)
ayman mounir
ayman mounir 2019년 7월 26일
편집: ayman mounir 2019년 7월 28일
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일
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일
[~, 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');

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by