# Extracting Numbers from a mixed string

2 vues (au cours des 30 derniers jours)
Hayden Garmon le 16 Juil 2020
Commenté : Hayden Garmon le 17 Juil 2020
Hi!
I would like to know how to extact the numbers from this string,
such that:
a = [ 3.00, 3.1390 , 0.0070, 1160]
b = [ 20.00. 20.1350, 0.012. 1323]
0070 , 1160
"Lung: 3.00 Calibrated: 3.1390 Uncertainty: 0.0070 Asset# 1160 Cal= N/A Due= N/A"
"Lung: 20.00 Calibrated: 20.1350 Uncertainty: 0.0120 Asset# 1323 Cal= N/A Due= N/A"
I cannot get the correct value with b=regexp(str,'\d?\.?\d+','match'), but I am close I think.
It works for a, but not for b
Cheers
HRG
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Kelly Kearney le 16 Juil 2020
Assuming numbers only appear as properly-formatted combos, you can simply look for all number-decimal groups:
b = regexp(str, '[\d\.]+', 'match');
This one is a little more picky, making sure the number-decimal groups form a "proper" number (i.e. wouldn't match something like 1.2.3)
b = regexp(str, '\d+\.{0,1}\d+', 'match');
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Hayden Garmon le 17 Juil 2020
Thanks Kelly!

Connectez-vous pour commenter.

### Plus de réponses (1)

jonas le 16 Juil 2020
Modifié(e) : jonas le 16 Juil 2020
out=regexp(b,'\d*\.?\d+','match')
Your expression will fail when there are more than 1 digits before the decimal sign, as ? is interpreted as "0 or 1" digit before the (optional) decimal sign
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by