user1020947
user1020947

Reputation:

matlab regular expression

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')

Upvotes: 3

Views: 2819

Answers (2)

niels
niels

Reputation: 760

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

Upvotes: 2

abcd
abcd

Reputation: 42225

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.

Upvotes: 7

Related Questions