how to use regexp for negative and positive integers?

12 ビュー (過去 30 日間)
dafniz91
dafniz91 2016 年 6 月 3 日
コメント済み: dafniz91 2016 年 6 月 3 日
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 件のコメント
dafniz91
dafniz91 2016 年 6 月 3 日
thank you, Andrei and Stephen, both of your answers were of use!

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

採用された回答

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 Center および 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