how to use regexp for negative and positive integers?

15 次查看(过去 30 天)
hello,
I have a trouble using regexp. I read a sensor value that can contain negative or positive integers and I need both. but until now I only get one of it(only positive integers o negative integers). here is my code that only reads positive integers:
FSR1 = regexp(FSR1,'\d+(\.)?(\d+)?','match');
and these are some types of integers that I`m reading:
'204'
'203'
'191'
'133'
'63'
'-28'
'-109'
'-145'
'-196'
'-134'
can someone help me, please?

采纳的回答

Andrei Bobrov
Andrei Bobrov 2016-6-3
编辑:Andrei Bobrov 2016-6-3
a = {'204'
'203'
'191'
'133'
'63'
'-28'
'-109'
'-145'
'-196'
'-134'}
FSR1 = str2double(regexp(a,'[-]?\d+(\.)?(\d+)?','match','once'))

更多回答(1 个)

Stephen23
Stephen23 2016-6-3
编辑:Stephen23 2016-6-3
This regular expression allows an optional +/- sign and decimal digits:
>> regexp(FSR1, '(+|-)?\d+(\.\d+)?', 'match', 'once')
ans =
'204'
'203'
'191'
'133'
'63'
'-28'
'-109'
'-145'
'-196'
'-134'

类别

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