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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.