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!