448191 Posted November 12, 2006 Share Posted November 12, 2006 I'm not that good with regex, so I'm hoping someone that is can help me out.I an attempt to reverse engineer ZF into UML diagrams, I need to match php5 class definitions in php files.Examples I want to match:[quote]abstract class Zend_Exception extends Exception {}[/quote][quote]abstract class Zend_Exception extends Exception { }[/quote][quote]class Zend_Exception extends Exception {}[/quote][quote]class Zend_Exception extends Exception { }[/quote][quote]final class Zend{ static private $_registry = array();[b] private function __construct() {}[/b] static public function loadInterface($interface, $dirs = null) { if (interface_exists($interface, false)) { return; } if (!interface_exists($interface, false)) { throw new Zend_Exception("File \"$file\" was loaded " . "but interface \"$interface\" was not found within."); } }}[/quote]The problem is I am sofar unable to distict between a method definition as in bold, and a class definition as in the first examples.I have tried negative lookbehind (?<!pattern), but even though it works just fine in RegexBuddy, php spits it out:[quote]Compilation failed: lookbehind assertion is not fixed length at offset 69[/quote]Here's what I use, this does everything I want it to in RegexBuddy:[code]'/^((abstract|final)?(\\s)*(class|interface)(\\s)+(\\w+)\\s*.*?)(?<!\\(\\)\\s*)(^\\}|^\\{\\\\s*}|{\\s*}$)/sm'[/code][b]EDIT:[/b]I more or less compromised, and made a separate expression:[code]<?php$result = preg_replace('/(\\([\\s\\w,\\$]*\\))(?:\\s)+(\\{)\\s*(\\})/m', '$1$2$3$4$5', $content);preg_match_all('/^((?:abstract|final)?\\s*(?:class|interface)\\s+\\w+\\s*.*?(?:^\\}|(?<!\\)){\\s*}))/sim',$result,$classdefs);?>[/code]It works for (almost) all classes now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.