How to extract strings with varied length by regular expression?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Yujian Wu
el 12 de En. de 2018
As the title, I'm trying to extract substrings from strings. But the substrings have different lengths. For example,
str1 = '2018_a_myid_the_here';
str2 = '2018_b_yourid_t_here';
What I want are 'myid' and 'yourid', which are of different length. So I used following code:
expression = '_(\w+)+_';
match = regexp(file_name, expression, 'match');
And I got the result as '_a_myid_the_' and '_b_yourid_t_'. Is there a better way to extract out 'myid' and 'yourid' directly without any further slicing?
Thank you!
0 comentarios
Respuesta aceptada
Stephen23
el 12 de En. de 2018
Editada: Stephen23
el 13 de En. de 2018
>> regexp('2018_a_myid_the_here','(?<=^\d+_[a-z]_)[a-z]+','match')
ans =
'myid'
>> regexp('2018_b_yourid_t_here','(?<=^\d+_[a-z]_)[a-z]+','match')
ans =
'yourid'
For developing and experimenting with regular expressions you might like to download my Interactive Regular Expression tool:
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!