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')
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With