matlab正则表达式

3 regex matlab

在以下字符串中:quantity = 100;我想使用正则表达式来获取100.

为什么下面的正则表达式没有返回100

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

abc*_*bcd 7

匹配任何数字的正则表达式是\d.因此,如果您的字符串只是表单text=numbers,那么以下内容将起作用.

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

result = 
         '100'
Run Code Online (Sandbox Code Playgroud)

请注意,MATLAB返回匹配的单元格数组.所以你不能使用'once'因为它只会返回1.