0

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 executeSomethingEdit ()
{

    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.

  • 1
    This is essentially a dupe of this: http://stackoverflow.com/questions/546433/regular-expression-to-match-outer-brackets – senderle Feb 22 '11 at 04:44
  • I think the regular expression are the bad tool for such a job. The best way to do it is something like this http://stackoverflow.com/questions/524548/regular-expression-to-detect-semi-colon-terminated-c-for-while-loops/524624#524624 – Xavier Combelle Feb 22 '11 at 15:32
  • True, Thanks for the links. shed some light in this issue. I opted for manually substitution in the end. This was faster, – Nanthini Muniapan Feb 24 '11 at 04:06

1 Answers1

0

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)
Adeel Zafar Soomro
  • 1,402
  • 9
  • 15