Why does this regex fail in Matlab (but not Octave or regex101.com)?
up vote
1
down vote
favorite
I have developed a regex (based on How do I use a Python regex to match the function syntax of MATLAB?) to try to capture the input and output arguments and function name from a matlab function declaration, here is the regex:
^s*functions+(?:(?:[((?:s*(?:w+[w0-9]*)s*,?)+)]|(w+[w0-9]*))s*=)?[s.]*(w+[w0-9]*)s*(?:(([^)]*)))?
You can see it in action here: regex at regex101.com
At regex101 it works perfectly, but in In matlab, when I try to use this regex, it doesn't work properly, for example, see the below matlab session:
K>> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr =
'function loss = conductionLo7ss (self, Irms, m, DPF)'
K>> fcn_decl_regex = '^s*functions+(?:(?:[((?: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'}
However, in Octave, it also works fine:
octave:8> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr = function loss = conductionLo7ss (self, Irms, m, DPF)
octave:9> fcn_decl_regex = '^s*functions+(?:(?:[((?: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)
}
In case it is helpful, the documentation for Matlab's regexp function is here
What the regex is actually trying to capture is the function input arguments (if there are any) as one block, the output arguments (if there are any) as one block, and the function name. So for example:
`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`
or
`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`
to give two examples
regex matlab octave
|
show 12 more comments
up vote
1
down vote
favorite
I have developed a regex (based on How do I use a Python regex to match the function syntax of MATLAB?) to try to capture the input and output arguments and function name from a matlab function declaration, here is the regex:
^s*functions+(?:(?:[((?:s*(?:w+[w0-9]*)s*,?)+)]|(w+[w0-9]*))s*=)?[s.]*(w+[w0-9]*)s*(?:(([^)]*)))?
You can see it in action here: regex at regex101.com
At regex101 it works perfectly, but in In matlab, when I try to use this regex, it doesn't work properly, for example, see the below matlab session:
K>> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr =
'function loss = conductionLo7ss (self, Irms, m, DPF)'
K>> fcn_decl_regex = '^s*functions+(?:(?:[((?: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'}
However, in Octave, it also works fine:
octave:8> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr = function loss = conductionLo7ss (self, Irms, m, DPF)
octave:9> fcn_decl_regex = '^s*functions+(?:(?:[((?: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)
}
In case it is helpful, the documentation for Matlab's regexp function is here
What the regex is actually trying to capture is the function input arguments (if there are any) as one block, the output arguments (if there are any) as one block, and the function name. So for example:
`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`
or
`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`
to give two examples
regex matlab octave
GNU Octave internally uses PCRE so it is no wonder that it works the same as your regex101 example where you've selected PCRE flavor
– Andy
Nov 20 at 9:46
It would help if you actually described what you're expecting the regular expression to identify, rather than just in relation to a specific example...
– Wolfie
Nov 20 at 9:49
Try with this modification. Also, if it does not work, try adding'emptymatch'
option. Or even with^s*functions+(?:(?:[([^]+)]|(w+))s*=)?[ .]*(w+)s*(?:(([^)]*)))?
.
– Wiktor Stribiżew
Nov 20 at 10:17
If that does not work, try defining named capturing groups,^s*functions+(?:(?:[(?<fname>[^]+)]|(?<fname>w+))s*=)?[ .]*(?<fname2>w+)s*(?:((?<args>[^)]*)))?
– Wiktor Stribiżew
Nov 20 at 10:43
2
It's expected but undesired behavior. regexp does not support nested tokens, even if they are non-capturing. Only the outermost tokens are captured. See the note in the doc: mathworks.com/help/matlab/matlab_prog/…
– matlabbit
Nov 21 at 20:39
|
show 12 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have developed a regex (based on How do I use a Python regex to match the function syntax of MATLAB?) to try to capture the input and output arguments and function name from a matlab function declaration, here is the regex:
^s*functions+(?:(?:[((?:s*(?:w+[w0-9]*)s*,?)+)]|(w+[w0-9]*))s*=)?[s.]*(w+[w0-9]*)s*(?:(([^)]*)))?
You can see it in action here: regex at regex101.com
At regex101 it works perfectly, but in In matlab, when I try to use this regex, it doesn't work properly, for example, see the below matlab session:
K>> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr =
'function loss = conductionLo7ss (self, Irms, m, DPF)'
K>> fcn_decl_regex = '^s*functions+(?:(?:[((?: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'}
However, in Octave, it also works fine:
octave:8> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr = function loss = conductionLo7ss (self, Irms, m, DPF)
octave:9> fcn_decl_regex = '^s*functions+(?:(?:[((?: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)
}
In case it is helpful, the documentation for Matlab's regexp function is here
What the regex is actually trying to capture is the function input arguments (if there are any) as one block, the output arguments (if there are any) as one block, and the function name. So for example:
`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`
or
`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`
to give two examples
regex matlab octave
I have developed a regex (based on How do I use a Python regex to match the function syntax of MATLAB?) to try to capture the input and output arguments and function name from a matlab function declaration, here is the regex:
^s*functions+(?:(?:[((?:s*(?:w+[w0-9]*)s*,?)+)]|(w+[w0-9]*))s*=)?[s.]*(w+[w0-9]*)s*(?:(([^)]*)))?
You can see it in action here: regex at regex101.com
At regex101 it works perfectly, but in In matlab, when I try to use this regex, it doesn't work properly, for example, see the below matlab session:
K>> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr =
'function loss = conductionLo7ss (self, Irms, m, DPF)'
K>> fcn_decl_regex = '^s*functions+(?:(?:[((?: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'}
However, in Octave, it also works fine:
octave:8> argstr = 'function loss = conductionLo7ss (self, Irms, m, DPF)'
argstr = function loss = conductionLo7ss (self, Irms, m, DPF)
octave:9> fcn_decl_regex = '^s*functions+(?:(?:[((?: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)
}
In case it is helpful, the documentation for Matlab's regexp function is here
What the regex is actually trying to capture is the function input arguments (if there are any) as one block, the output arguments (if there are any) as one block, and the function name. So for example:
`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`
or
`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`
to give two examples
regex matlab octave
regex matlab octave
edited Nov 20 at 11:07
asked Nov 20 at 9:43
crobar
1,11021431
1,11021431
GNU Octave internally uses PCRE so it is no wonder that it works the same as your regex101 example where you've selected PCRE flavor
– Andy
Nov 20 at 9:46
It would help if you actually described what you're expecting the regular expression to identify, rather than just in relation to a specific example...
– Wolfie
Nov 20 at 9:49
Try with this modification. Also, if it does not work, try adding'emptymatch'
option. Or even with^s*functions+(?:(?:[([^]+)]|(w+))s*=)?[ .]*(w+)s*(?:(([^)]*)))?
.
– Wiktor Stribiżew
Nov 20 at 10:17
If that does not work, try defining named capturing groups,^s*functions+(?:(?:[(?<fname>[^]+)]|(?<fname>w+))s*=)?[ .]*(?<fname2>w+)s*(?:((?<args>[^)]*)))?
– Wiktor Stribiżew
Nov 20 at 10:43
2
It's expected but undesired behavior. regexp does not support nested tokens, even if they are non-capturing. Only the outermost tokens are captured. See the note in the doc: mathworks.com/help/matlab/matlab_prog/…
– matlabbit
Nov 21 at 20:39
|
show 12 more comments
GNU Octave internally uses PCRE so it is no wonder that it works the same as your regex101 example where you've selected PCRE flavor
– Andy
Nov 20 at 9:46
It would help if you actually described what you're expecting the regular expression to identify, rather than just in relation to a specific example...
– Wolfie
Nov 20 at 9:49
Try with this modification. Also, if it does not work, try adding'emptymatch'
option. Or even with^s*functions+(?:(?:[([^]+)]|(w+))s*=)?[ .]*(w+)s*(?:(([^)]*)))?
.
– Wiktor Stribiżew
Nov 20 at 10:17
If that does not work, try defining named capturing groups,^s*functions+(?:(?:[(?<fname>[^]+)]|(?<fname>w+))s*=)?[ .]*(?<fname2>w+)s*(?:((?<args>[^)]*)))?
– Wiktor Stribiżew
Nov 20 at 10:43
2
It's expected but undesired behavior. regexp does not support nested tokens, even if they are non-capturing. Only the outermost tokens are captured. See the note in the doc: mathworks.com/help/matlab/matlab_prog/…
– matlabbit
Nov 21 at 20:39
GNU Octave internally uses PCRE so it is no wonder that it works the same as your regex101 example where you've selected PCRE flavor
– Andy
Nov 20 at 9:46
GNU Octave internally uses PCRE so it is no wonder that it works the same as your regex101 example where you've selected PCRE flavor
– Andy
Nov 20 at 9:46
It would help if you actually described what you're expecting the regular expression to identify, rather than just in relation to a specific example...
– Wolfie
Nov 20 at 9:49
It would help if you actually described what you're expecting the regular expression to identify, rather than just in relation to a specific example...
– Wolfie
Nov 20 at 9:49
Try with this modification. Also, if it does not work, try adding
'emptymatch'
option. Or even with ^s*functions+(?:(?:[([^]+)]|(w+))s*=)?[ .]*(w+)s*(?:(([^)]*)))?
.– Wiktor Stribiżew
Nov 20 at 10:17
Try with this modification. Also, if it does not work, try adding
'emptymatch'
option. Or even with ^s*functions+(?:(?:[([^]+)]|(w+))s*=)?[ .]*(w+)s*(?:(([^)]*)))?
.– Wiktor Stribiżew
Nov 20 at 10:17
If that does not work, try defining named capturing groups,
^s*functions+(?:(?:[(?<fname>[^]+)]|(?<fname>w+))s*=)?[ .]*(?<fname2>w+)s*(?:((?<args>[^)]*)))?
– Wiktor Stribiżew
Nov 20 at 10:43
If that does not work, try defining named capturing groups,
^s*functions+(?:(?:[(?<fname>[^]+)]|(?<fname>w+))s*=)?[ .]*(?<fname2>w+)s*(?:((?<args>[^)]*)))?
– Wiktor Stribiżew
Nov 20 at 10:43
2
2
It's expected but undesired behavior. regexp does not support nested tokens, even if they are non-capturing. Only the outermost tokens are captured. See the note in the doc: mathworks.com/help/matlab/matlab_prog/…
– matlabbit
Nov 21 at 20:39
It's expected but undesired behavior. regexp does not support nested tokens, even if they are non-capturing. Only the outermost tokens are captured. See the note in the doc: mathworks.com/help/matlab/matlab_prog/…
– matlabbit
Nov 21 at 20:39
|
show 12 more comments
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390153%2fwhy-does-this-regex-fail-in-matlab-but-not-octave-or-regex101-com%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390153%2fwhy-does-this-regex-fail-in-matlab-but-not-octave-or-regex101-com%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
GNU Octave internally uses PCRE so it is no wonder that it works the same as your regex101 example where you've selected PCRE flavor
– Andy
Nov 20 at 9:46
It would help if you actually described what you're expecting the regular expression to identify, rather than just in relation to a specific example...
– Wolfie
Nov 20 at 9:49
Try with this modification. Also, if it does not work, try adding
'emptymatch'
option. Or even with^s*functions+(?:(?:[([^]+)]|(w+))s*=)?[ .]*(w+)s*(?:(([^)]*)))?
.– Wiktor Stribiżew
Nov 20 at 10:17
If that does not work, try defining named capturing groups,
^s*functions+(?:(?:[(?<fname>[^]+)]|(?<fname>w+))s*=)?[ .]*(?<fname2>w+)s*(?:((?<args>[^)]*)))?
– Wiktor Stribiżew
Nov 20 at 10:43
2
It's expected but undesired behavior. regexp does not support nested tokens, even if they are non-capturing. Only the outermost tokens are captured. See the note in the doc: mathworks.com/help/matlab/matlab_prog/…
– matlabbit
Nov 21 at 20:39