[英]How can I extract a file name based on number string?
我在結構數組中有一個文件名列表,例如:
4x1 struct array with fields:
name
date
bytes
isdir
datenum
其中files.name
ans =
ts.01094000.crest.csv
ans =
ts.01100600.crest.csv
等等
我還有另一個數字列表(例如1094000)。 我想從結構中找到相應的文件名。
請注意,1094000的前面沒有0。通常可能還有其他數字。 所以我想搜索“ 1094000”並找到該名稱。
我知道我可以使用Regex做到這一點。 但是我以前從未使用過。 並且發現很難使用strfind來寫數字而不是文本。 歡迎任何建議或其他方法。
我嘗試過的
regexp(files.name,'ts.(\d*)1094000.crest.csv','match');
我認為您想要的正則表達式更像
filenames = {'ts.01100600.crest.csv','ts.01094000.crest.csv'};
matches = regexp(filenames, ['ts\.0*' num2str(1094000) '\.crest\.csv']);
matches = ~cellfun('isempty', matches);
filenames(matches)
對於具有strfind的解決方案...
16b之前:
match = ~cellfun('isempty', strfind({files.name}, num2str(1094000)),'UniformOutput',true)
files(match)
16b +:
match = contains({files.name}, string(1094000))
files(match)
但是,如果要查找的數字存在於意外位置,例如在[“ 01000”,“ 00101”]中尋找10,則strfind方式可能會出現問題。
如果文件名與ts.NUMBER.crest.csv模式匹配,則在16b +中,您可以執行以下操作:
str = {files.name};
str = extractBetween(str,4,'.');
str = strip(str,'left','0');
matches = str == string(1094000);
files(matches)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.