I've got a long PHP "class" with many if blocks that I want to transform with a Python script. So I want to match every "parent level" if-block and pass it as item into a list for further processing.
pattern = re.compile(r'^if[^(\n\})]*\}$', re.M)
result = re.findall(pattern, filecontent)
So I tried many variants now, without success so far. Of course we have the case of nested if statements in those if blocks. So we can't just match until the next closing curly brace.
Luckily the code is cleanly formatted so I could match over multiple lines starting from a line beginning with 'if' and ending with a closing curly brace at the start of a line.
^if // line starting with 'if'
[^(\n\})]* // followed by zero or more characters that are not
// a closing curly brace preceded by a newline character
\n\}$ // ending with a closing curlybrace preceded by \n
This approach seems to be my best try. But it does not match anything.
Here's some snippet for testing, processing it should result in 3 list items: https://pastebin.com/2WHC1k6f
Aucun commentaire:
Enregistrer un commentaire