MATLAB中的多个捕获组 [英] Multiple capture groups in MATLAB

查看:70
本文介绍了MATLAB中的多个捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数字或字母a的字符串,可能后跟rl.

I have a string that either has a number or the letter a, possibly followed byr or l.

在MATLAB中,以下正则表达式返回为

In MATLAB the following regexp returns as

>> regexp('10r', '([0-9]*|a)(l|r)*', 'match')
ans = 
    '10r'

我会分别期望10r,因为我有两个捕获组.有没有办法让单元格数组都独立返回?我在文档中看不到它.

I would expect 10 and r separately, because I have two capture groups. Is there a way to get a cell array with both returned independently? I can't see it in the documentation.

推荐答案

您想要令牌"而不是匹配"

You want 'tokens' instead of 'match'

>> toks = regexp('10r', '([0-9]*|a)(l|r)*', 'tokens');
>> toks{1}
ans = 
    '10'    'r'

或者,如果您想花哨的话,给令牌命名并获得一个结构数组:

Or if you want to get fancy, name the tokens and get a struct array:

>> toks = regexp('10r', '(?<number>[0-9]*|a)(?<letter>l|r)*', 'names');
>> toks
toks = 
    number: '10'
    letter: 'r'

这篇关于MATLAB中的多个捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆