我已经开发了一个正则表达式(基于How do I use a Python regex to match the function syntax of MATLAB?)来尝试从matlab函数声明中捕获输入和输出参数以及函数名,这是正则表达式:
^\s*function\s+(?:(?:\[((?:\s*(?:\w+[\w0-9]*)\s*,?)+)\]|(\w+[\w0-9]*))\s*=)?[\s.]*(\w+[\w0-9]*)\s*(?:\(([^)]*)\))?
您可以在此处查看它的运行情况:regex at regex101.com
在regex101上它可以正常工作,但是在matlab中,当我尝试使用此regex时,它不能正常工作,例如,请参见下面的matlab会话:
K>> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr =
'function loss = conductionLo7ss (self, Irms, m, DPF)'
K>> fcn_decl_regex = '^\s*function\s+(?:(?:\[((?:\s*(?:\w+[\w0-9]*)\s*,?)+)\]|(\w+[\w0-9]*))\s*=)?[\s.]*(\w+[\w0-9]*)\s*(?:\(([^)]*)\))?';
K>> [tokens, match] = regexp (argstr, fcn_decl_regex, 'tokens', 'match')
tokens =
1×1 cell array
{1×1 cell}
match =
1×1 cell array
{'function loss = conductionLo7ss (self, Irms, m, DPF)'}
K>> tokens{1}
ans =
1×1 cell array
{'conductionLo7ss'}
但是,在Octave中,它也可以正常工作:
octave:8> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr = function loss = conductionLo7ss (self, Irms, m, DPF)
octave:9> fcn_decl_regex = '^\s*function\s+(?:(?:\[((?:\s*(?:\w+[\w0-9]*)\s*,?)+)\]|(\w+[\w0-9]*))\s*=)?[\s.]*(\w+[\w0-9]*)\s*(?:\(([^)]*)\))?';
octave:10> [tokens, match] = regexp (argstr, fcn_decl_regex, 'tokens', 'match')
tokens =
{
[1,1] =
{
[1,1] = loss
[1,2] = conductionLo7ss
[1,3] = self, Irms, m, DPF
}
}
match =
{
[1,1] = function loss = conductionLo7ss (self, Irms, m, DPF)
}
如果有帮助,Matlab的regexp函数的文档为here
正则表达式实际上试图捕获的是函数输入参数(如果有)作为一个块,输出参数(如果有)作为一个块,以及函数名。例如:
`function loss = conductionLo7ss (self, Irms, m, DPF)`
Group 2. 9-13 `loss`
Group 3. 16-31 `conductionLo7ss`
Group 4. 33-51 `self, Irms, m, DPF`
或
`function [loss, arg2] = conductionLoss (self, Irms, m, DPF)`
Group 1. 63-73 `loss, arg2`
Group 3. 77-91 `conductionLoss`
Group 4. 93-111 `self, Irms, m, DPF`
举两个例子