extract numbers from a string.
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jasmine Zhu
am 12 Sep. 2022
Kommentiert: Jasmine Zhu
am 13 Sep. 2022
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 Kommentar
Star Strider
am 12 Sep. 2022
Something is wrong with that because it throws this error —
str = "sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}
Akzeptierte Antwort
Voss
am 12 Sep. 2022
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
numbers = regexp(str,'"(\d+)"','tokens');
numbers = [numbers{:}]
numbers = str2double(numbers)
Weitere Antworten (1)
Hiro Yoshino
am 12 Sep. 2022
You can use pattern:
pat = digitsPattern(4); % Pattern
str = '"sensor_serials": {"H1": "3637", "H2": "3638", "H3": "2787"}'
numbers = extract(str,pat);
string(numbers)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!