开发者

Regex to capture the codes between specific function

I have been experimenting with python re, trying to capture specific variables between specific functions. To illustrate let me give an example of file contents of a php file :-

public function executeSomething ()
{        
    $this->title =  'Edit something';
    $this->action = 'Edit';
    $this->headerTitle = 'Edit something';

    return true;
}

public function executeSomet开发者_如何学运维hingEdit ()
{

    if (strlen ($this->somethingElse) > 0)
    {
        $this->titleText = "Update";    
        $title = 'Edit something';
    }
    else
    {
        $this->titleText = "Create";    
        $title = 'Create something';
    }

    $this->title =  $title;   
    $this->headerTitle = $title; 
    $this->formTitle = 'Something details'

    return true;
}

What the python script needs to do now is iterate through this file in search for functions that starts with 'public function execute' and get the content within the braces i.e { }. I have already came up with python code to achieve this i.e :-

 r = re.compile(r"public function execute(.*?)\(\).*?{(.*?)}", re.DOTALL)

The problem occurs when I have a validation within the function i.e if else statement such as the one in the function executeSomethingEdit. The script doesn't takes into account whatever codes below the if statements closing braces '}'. Therefore I need to further enhance the python code to include the function declaration below i.e something like this :-

r = re.compile(r"public function execute(.*?)\(\).*?{(.*?)}.*?public function", re.DOTALL)

At the moment this code is not working/producing the result that I wanted. I need to used python's re specifically because i need to further analyse the content of {(.*?)}. I'm very new to python so I hope someone could direct me in the right direction or at least tell me what I'm doing wrong. Thanks in advance.


If the input PHP has no bugs and has consistent indentation, you could check for a non-space character before the closing brace.

r = re.compile(r'public function execute(.*?)\(\).*?{(.*?)[^ ]}', re.DOTALL)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜