Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Regex Help

  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    Regex Help

    I'm trying to create a regex to capture the comment blocks in a php file. Specifically blocks like:

    PHP Code:
    /**
    * some text here
    * and some more
    * etc
    */
    function here()
    {
    }

    /**
    * some text here
    * and some more
    * etc
    */ 
    Basically a block that starts with /** and ends with */ and can have any text in the middle

    I tried this: /\*\*[.\r\n]+\*/ but that didn't work.

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    I got it working but its a bit of a cludge - I used [^~] (on the grounds I wont be using ~ in my comments) instead of [.\r\n], for some reason . wasn't working in a character class.

    This is what I'm currently using but I'd still like to fix it to be less cludgy.

    /\*\*([^~]*?)\*/

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ah, too much work. Trust me, I tried before php 5 and I hated it I recall.
    I'm pretty sure you said your on php 5+ now right? Look into the 'Reflection' class in php, one of them is the ReflectionFunction, which will drag all the info out for you (where comment is one of its own so long as its /** .. */ style - only two stars at the start).
    Try that nancy, you'll love all the other stuff it does too!

    Edit:
    Here's a link directly to ReflectionFunction
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    try /\/\*\*(.|[\r\n])+?\*\//g
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    If you want it less cludgy you could just use /\*\*.+?\*/ with the s modifier.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    As far as I know can't be done with a single regex. A solution, if this help you is that:
    PHP Code:
    <?php
    $texts 
    "/**
    * some text here
    * and some more
    * etc
    */
    function here()
    {
    }

    /**
    * some text here
    * and some more
    * etc
    */  

    /* Another style
     *
     */
    "
    ;

    function 
    markblock($s){
      
    $r preg_split("/\*\//",$s);
      return array(
    "cmt" => $r);
    }

    $comments preg_split("/\/\*\*?\s*/",$texts);
    $comments array_map("markblock",$comments);
    var_dump($comments);
    ?>
    you can format the comments or sections for example to replace * and spaces/newlines with a single space....

    to find and list comment sections
    PHP Code:
    foreach($comments as $blk){
      echo 
    $blk['cmt'][0]."\n--------------\n";

    same thing with sections what follow comments
    PHP Code:
    foreach($comments as $blk){
      echo 
    $blk['cmt'][1]."\n--------------\n";
    }
    ?> 
    regards

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I'm pretty sure when I used this method I did it in a single regexp. Let me think...
    PHP Code:
    /**
     * Function: MyFunc
     * All that fun stuff here
     */
    ... 
    PHP Code:
    ...
    $matches = array();
    preg_match('/\/\*\*(.*)\*\//ms'$inputStream$matches);
    $i 0;
    foreach (
    $matches AS $vals)
    {
        
    // Yes I see what you're getting at now, this may not work.  Thats ok.
        
    if ($i++ == 0)
        {
            continue;
        }
        
    // Do whatever you want but you'll need...
        
    $vals trim($vals' *');

    Edit:
    I see what you mean now oesxyl, this may not work. I'm thinking that the resultant will always have too much data in it.


    I just can't remember off hand if I used an ungreedy quantifier on it or not, I'm thinking that I did or I should only get one set of comments which would include tons of code in it as well.
    This of course was only for doc style comments (/** ... */), with only the two leading stars (just like the reflection, it would fail if there were more)
    Reflection usage (if you have it, its so much easier):
    PHP Code:
    /**
    * some text here
    * and some more
    * etc
    */
    function here()
    {
    }

    /**
    * some text here
    * and some more
    * etc
    */ 
    Now, you can either use the reflection on an individual function, or collect all that are defined:
    PHP Code:
    // Lets get all the user defined ones by forcing the script to give it to us:
    $myComments = array();
    $defined get_defined_functions(); // Built in since php 4.0.4
    if (is_array($defined['user']))
    {
        foreach (
    $defined['user'] AS $userFunc)
        {
            
    $reflect = new ReflectionFunction($userFunc);
            
    $myComments[$userFunc] = var_export($reflect->getDocComment(), 1);
        }

    That should give you an array of your comments with an associative function name in mycomments. There is a whole pile of stuff you can get with Reflection, and its awesome for TDD if you're into that
    PHP Code:

    <?php
    class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector
    {
        final private 
    __clone()
        public 
    void __construct(string name)
        public 
    string __toString()
        public static 
    string export(string namebool return)
        public 
    string getName()
        public 
    bool isInternal()
        public 
    bool isDisabled()
        public 
    bool isUserDefined()
        public 
    string getFileName()
        public 
    int getStartLine()
        public 
    int getEndLine()
        public 
    string getDocComment()
        public array 
    getStaticVariables()
        public 
    mixed invoke([mixed args [, ...]])
        public 
    mixed invokeArgs(array args)
        public 
    bool returnsReference()
        public 
    ReflectionParameter[] getParameters()
        public 
    int getNumberOfParameters()
        public 
    int getNumberOfRequiredParameters()
    }
    ?>
    Been awhile since I've reflected too, so you may need to tweek it a bit.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Fou-Lu View Post
    That should give you an array of your comments with an associative function name in mycomments. There is a whole pile of stuff you can get with Reflection, and its awesome for TDD if you're into that
    probably are more solution to this problems. I don't look for some parser extensions for php, for example, .
    But all this depends of what NancyJ want to achive. If want to build some CASE for php, probably the best solution is reflection.
    If only want to extract the comments block, a documenting system, one solution look like mine or better a already build package like doxygen( I don't know if there is a version for windows but I guess it is).

    regards

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by oesxyl View Post
    probably are more solution to this problems. I don't look for some parser extensions for php, for example, .
    But all this depends of what NancyJ want to achive. If want to build some CASE for php, probably the best solution is reflection.
    If only want to extract the comments block, a documenting system, one solution look like mine or better a already build package like doxygen( I don't know if there is a version for windows but I guess it is).

    regards
    Agreed. PHP is so expansive I don't think I'll ever get to half of the built in stuff, even with 8 years of usage >.<. I suggest reflection simply because I do so much object work now - so easy to build my documentation up, otherwise I'd be too lazy to document my methods, lol.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •