Extracting a number from a sentence string
显示 更早的评论
I am trying to extract numbers from a sentence string through a function that does not know the exact placement or how many numbers are in the string.
for example my input would be
a='my weight is between 200 and 250 lbs'
or it could be
a='my weight is 200 lbs'
the weight will always be before "lbs", but the length of weight string is unknown.
I am having trouble using the sscanf function to make a universal code to extract the numbers from a string. Any help is greatly appreciated.
回答(1 个)
per isakson
2015-2-8
编辑:per isakson
2015-2-8
A start
>> a='my weight is between 200 and 250 lbs'
a =
my weight is between 200 and 250 lbs
>> cac = regexp( a, '\d++', 'match' )
cac =
'200' '250'
Warning: This will not match '1.2E-003', not even '-4' :-(
类别
在 帮助中心 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!