Jump to content

Pulling my hair out.


448191

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/26998-pulling-my-hair-out/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.