extract numbers from a string.
9 ビュー (過去 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!