regexp string to numeric array

3 ビュー (過去 30 日間)
Knut
Knut 2016 年 11 月 4 日
回答済み: Stephen23 2023 年 4 月 20 日
I have what seems like a common problem: a series of strings containing numeric data mixed with text. I want to extract those numbers into vectors or arrays. I have come up with the mock-up below, but it feels like a cludge. Is there a simple, readable one-liner that does the same?
str = {'bob22alice666buster2', 'donald42lisa00buddy9'};
pat = '\w*(\d{2})\w*(\d{2,3})\w*(\d{1})';
for idx = 1:2
tmp = regexp(str{idx}, pat, 'tokens');
male(idx) = str2double(tmp{1}(1));
female(idx) = str2double(tmp{1}(2));
doggie(idx) = str2double(tmp{1}(3));
end
I was hoping for something ala:
for ...
Mx3arr = str2num(regexp(str{idx}, pat, 'tokens')');
end

回答 (2 件)

Jan
Jan 2016 年 11 月 4 日
編集済み: Jan 2016 年 11 月 4 日
str = {'bob22alice666buster2', 'donald42lisa00buddy9'};
pat = '\w*(\d{2})\w*(\d{2,3})\w*(\d{1})';
tmp = regexp(str, pat, 'tokens');
C1 = cat(1, tmp{:});
C2 = cat(1, C1{:});
M = str2double(C2);
I still prefer the dull string parsing without regexp:
S = sprintf('%s ', str{:});
S(isstrprop(S, 'alpha')) = ' ';
M = sscanf(S, '%d', [3, inf]);
Although it looks less smart, it works without clutter and much faster.

Stephen23
Stephen23 2023 年 4 月 20 日
Assuming that every string contains exactly the same number of numeric data:
str = {'bob22alice666buster2', 'donald42lisa00buddy9'};
tmp = regexp(str,'\d+','match');
mat = str2double(vertcat(tmp{:}))
mat = 2×3
22 666 2 42 0 9

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by