specific pattern from the file name

4 visualizaciones (últimos 30 días)
ayman mounir
ayman mounir el 26 de Jul. de 2019
Editada: ayman mounir el 28 de Jul. de 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

Respuesta aceptada

per isakson
per isakson el 26 de Jul. de 2019
Editada: per isakson el 26 de Jul. de 2019
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 comentario
ayman mounir
ayman mounir el 27 de Jul. de 2019
Editada: ayman mounir el 28 de Jul. de 2019
Thanks It works perfectly

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 26 de Jul. de 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');

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by