Write a function to extracts number between 2 letters?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to write a MATLAB code that extracts a 3 digit number in between two letters. the tricky part is that the letter after the number could be either (s)or(h)
E.g 1 the filename is 'rfc_f123sars.tif' this should extract 123 which is between _f and s E.g 2 the filename is 'rfc_f456hars.tif' this should extract 456 which is between _f and h Thanks :)
0 个评论
采纳的回答
per isakson
2016-9-7
编辑:per isakson
2016-9-7
Without comments
>> str = 'rfc_f123sars.tif' ;
>> regexp( str, '(?<=_f)\d{3}(?=[sh]{1})', 'match', 'once' )
ans =
123
and slightly different; replace [sh]{1} by (s|h), which is a tiny bit cleaner
>> str = 'rfc_f456hars.tif';
>> regexp( str, '(?<=_f)\d{3}(?=(s|h))', 'match', 'once' )
ans =
456
If regexp returns empty set out=0
4 个评论
更多回答(2 个)
Walter Roberson
2016-9-7
regexp(filename, '\d\d\d', 'match')
or if you want to be more secure,
regexp(filename, '(?<=[A-Za-z])\d\d\d(?=[A-Za-z])', 'match')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!