How to use Python regex to match MATLAB function syntax?

I am trying to find all the inputs / outputs of all MATLAB functions in our internal library. I am new (first time) to regex and am trying to use multi-line mode in the Python library re.

The syntax of the MATLAB function is as follows:

function output = func_name(input)

where the signature can span multiple lines.

I started with a template like:

re.compile(r"^.*function (.*)=(.*)\([.\n]*\)$", re.M)

but I keep getting unsupported template statement error. Any pointer is appreciated!

EDIT:

Now I have:

pattern = re.compile(r"^\s*function (.*?)= [\w\n.]*?\(.*?\)", re.M|re.DOTALL)

which gives matches:

        function [fcst, spread] = ...
                VolFcstMKT(R,...
                           mktVol,...
                           calibrate,...
                           spread_init,...
                           fcstdays,...
                           tsperyear)

        if(calibrate)
            if(nargin < 6)
                tsperyear = 252;
            end
            templen = length(R)

My question is why does it give extra lines instead of stopping at the first )?

+3
source share
3 answers

() , re.T re.M, re.compile (re.template - - , , , REs ). print re.M , , re.compile?

RE (: input , , re.DOTALL ) - - .

: , ( Q), OP: re.DOTALL|re.MULTINE, '$' , - ( .* .*? ), , swathe... , . , Q : , , , , ..

+5

, MATLAB m:

^\s*function\s+((\[[\w\s,.]*\]|[\w]*)\s*=)?[\s.]*\w+(\([^)]*\))?

:

^\s*             # Match 0 or more whitespace characters
                 #    at the start
function         # Match the word function
\s+              # Match 1 or more whitespace characters
(                # Start grouping 1
 (               # Start grouping 2
  \[             # Match opening bracket
  [\w\s,.]*      # Match 0 or more letters, numbers,
                 #    whitespace, underscores, commas,
                 #    or periods...
  \]             # Match closing bracket
  |[\w]*         # ... or match 0 or more letters,
                 #    numbers, or underscores
 )               # End grouping 2
 \s*             # Match 0 or more whitespace characters
 =               # Match an equal sign
)?               # End grouping 1; Match it 0 or 1 times
[\s.]*           # Match 0 or more whitespace characters
                 #    or periods
\w+              # Match 1 or more letters, numbers, or
                 #    underscores
(                # Start grouping 3
 \(              # Match opening parenthesis
 [^)]*           # Match 0 or more characters that
                 #    aren't a closing parenthesis
 \)              # Match closing parenthesis
)?               # End grouping 3; Match it 0 or 1 times

, , MATLAB. :

function [out1,out2,...] = func_name(in1,in2,...)

, :

function func_name                 %# No inputs or outputs
function func_name(in1)            %# 1 input
function func_name(in1,in2)        %# 2 inputs
function out1 = func_name          %# 1 output
function [out1] = func_name        %# Also 1 output
function [out1,out2] = func_name   %# 2 outputs
...

(...) , , :

function out1 = ...
    func_name(in1,...
              in2,...
              in3)

, :

function func_name(varargin)       %# Any number of inputs possible
function func_name(in1,~,in3)      %# Second of three inputs is ignored

, m 1 , , , ( ).

+2

What about regular Python string operations? Only an example only

for line in open("file"):
    sline=line.strip()
    if sline.startswith("function"):
       lhs,rhs =sline.split("=")
       out=lhs.replace("function ","")
       if "[" in out and "]" in out:
          out=out.replace("]","").replace("[","").split(",")
       print out
       m=rhs.find("(")
       if m!=-1:
          rhs=rhs[m:].replace(")","").replace("(","").split(",")           
       print rhs
Output example

$ cat file
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
function mean = avg(x,n)
mean = sum(x)/n;
$ python python.py
['mean', 'stdev ']
[' statx']
mean
[' avgx', 'n']

Of course, there should be many other scripts for declaring functions in Matlab, such as function nothing, function a = betc., so add these checks yourself.

0
source

Source: https://habr.com/ru/post/1730932/


All Articles