How to extract only floating numbers from a string

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

1 件のコメント

Jan
Jan 2018 年 6 月 20 日
What's about '1e-5', '1D+004', '.1'?

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2018 年 6 月 20 日

2 投票

>> 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 件のコメント

parmeshwar prasad
parmeshwar prasad 2018 年 7 月 18 日
Thank you very much. Your code solved my problem.
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 日

0 投票

C = regexp(S,'[-+]?\d+\.?\d*','match');

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by