How to extract only floating numbers from a string

조회 수: 12 (최근 30일)
parmeshwar prasad
parmeshwar prasad 2018년 6월 20일
답변: Riadh Essaadali 2023년 5월 21일
Here is my string "21.5VgDC_0.05000V_VgAC_50M-150M30ms47GV1" How can I extract only the numbers 21.5, 0.05000, 50, 150 30 and 47 from the string. Thanks in advance

채택된 답변

Stephen23
Stephen23 2018년 6월 20일
>> S = '21.5VgDC_0.05000V_VgAC_50M-150M30ms47GV1';
>> C = regexp(S,'\d+\.?\d*','match');
>> C{:}
ans = 21.5
ans = 0.05000
ans = 50
ans = 150
ans = 30
ans = 47
ans = 1
  댓글 수: 3
Nadatimuj
Nadatimuj 2022년 3월 9일
편집: Nadatimuj 2022년 3월 9일
What if I use C = regexp(S,'\d*\.?\d*','match')?
And what if I use C = regexp(S,'\d*\.?\d+','match')
What is the difference?
Walter Roberson
Walter Roberson 2022년 3월 9일
In the first one everything is optional, so it matches the empty pattern too.
The second one does require at least one digit. However it does not support digits followed by a decimal point with no digits after

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Riadh Essaadali
Riadh Essaadali 2023년 5월 21일
C = regexp(S,'[-+]?\d+\.?\d*','match');

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by