Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matlab regular expression

Tags:

regex

matlab

In the following string: quantity = 100; I would like to use a regex in order to get 100.

Why doesn't the following regex return 100??

regexp('quantity=100;','(?=\w*\s*\=\s*)[^]+(?=\s*;$)','match','once')


2 Answers

The regex to match any digit is \d. So if your strings are only of the form text=numbers, then the following will work.

digits = regexp( 'quantity=100;', '\d', 'match');
result = [digits{:}]

result = 
         '100'

Note that MATLAB returns a cell array of matches. So you can't use 'once' because it will return only 1.

like image 163
abcd Avatar answered Dec 23 '25 00:12

abcd


You should use a negative look ahead regex in the beginning, try this:

regexp('quantity=100;','(?<=\w*\s*\=\s*)[^]+(?=\s*;$)','match','once')

or

regexp( 'quantity=100;', '(?<=^.*\=\s*)(.*)(?=\s*;$)', 'match', 'once' ) which is much simpler

like image 43
niels Avatar answered Dec 22 '25 23:12

niels



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!