how to use regexp for negative and positive integers?

16 visualizaciones (últimos 30 días)
dafniz91
dafniz91 el 3 de Jun. de 2016
Comentada: dafniz91 el 3 de Jun. de 2016
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?
  1 comentario
dafniz91
dafniz91 el 3 de Jun. de 2016
thank you, Andrei and Stephen, both of your answers were of use!

Iniciar sesión para comentar.

Respuesta aceptada

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

Más respuestas (1)

Stephen23
Stephen23 el 3 de Jun. de 2016
Editada: Stephen23 el 3 de Jun. de 2016
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'

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by