extract numbers from a string.
12 次查看(过去 30 天)
显示 更早的评论
str = "sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"},
I would like to extract the number 3637, 3638, and 2787 from the above string. How should I do it? Thanks!
1 个评论
Star Strider
2022-9-12
Something is wrong with that because it throws this error —
str = "sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}
采纳的回答
Voss
2022-9-12
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
numbers = regexp(str,'"(\d+)"','tokens');
numbers = [numbers{:}]
numbers = str2double(numbers)
更多回答(1 个)
Hiro Yoshino
2022-9-12
You can use pattern:
pat = digitsPattern(4); % Pattern
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
numbers = extract(str,pat);
string(numbers)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!