Results 1 to 5 of 5

Thread: [solved] RegEx for comments

  1. #1

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Resolved [solved] RegEx for comments

    How do I catch up the C# comments?
    Code:
    // This is a comment
    /// This is a comment
    /* This
       is
       a
       comment
    */
    Code:
            $comments="/(\/\/.*\n)/";
            $string=preg_replace($comments,"<span style='color:#007700'>$1</span>",$string);
    Didn't work. Any help is greatly appreciated. Thank you.
    Last edited by brown monkey; Oct 1st, 2004 at 03:23 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I would recommend you do it in two steps, once for the // and once for the /* */ comments.

    Code:
    cpp_comment = '|(//.*)\n|';
    $string=preg_replace($cpp_comment,"<span style='color:#007700'>$1</span>\n",$string);
    c_comment = '|(/\*.*\*/)|s';
    $string=preg_replace($c_comment,"<span style='color:#007700'>$1</span>",$string);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Thank you CornedBee.

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Don't forget to turn off the greediness:
    PHP Code:
    c_comment '/(\\/\\*.*\\*\\/)/sU'
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    Ok. I'll remember that. Thank you again. Thank you.

Posting Permissions

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



Click Here to Expand Forum to Full Width