Regular expression to get multi line comment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    #1

    Regular expression to get multi line comment

    Hello all,

    If I have the following code fragment:
    /*
    comment bla bla
    */
    ....code...

    With a regular expression, how do I get/extract the comment inside this
    multi line comment block. With and without the comment characters?
    And how do I get all the multi line comment from in the entire script?
    (yes I have red the documentation, but obviously this wasn't enough ;-) )


    Thanks in advance
    Rob








  • Daniel Tryba

    #2
    Re: Regular expression to get multi line comment

    In comp.lang.php Rob <reply@newsgrou p.nl> wrote:[color=blue]
    > If I have the following code fragment:
    > /*
    > comment bla bla
    > */
    > ...code...
    >
    > With a regular expression, how do I get/extract the comment inside this
    > multi line comment block. With and without the comment characters?
    > And how do I get all the multi line comment from in the entire script?
    > (yes I have red the documentation, but obviously this wasn't enough ;-) )[/color]

    What documentation did you read? AFAIK there is mention of a multiline
    flag in the preg section. So all you do need to do is do a nongreedy
    multiline match for /\/\*.*?/*///.

    FUP comp.lang.php

    Comment

    • Rob

      #3
      Re: Regular expression to get multi line comment


      "Daniel Tryba" <partmapsswen@i nvalid.tryba.nl > schreef in bericht
      news:424133d0$0 $155$c5fe704e@n ews6.xs4all.nl. ..[color=blue]
      > In comp.lang.php Rob <reply@newsgrou p.nl> wrote:[color=green]
      >> If I have the following code fragment:
      >> /*
      >> comment bla bla
      >> */
      >> ...code...
      >>
      >> With a regular expression, how do I get/extract the comment inside this
      >> multi line comment block. With and without the comment characters?
      >> And how do I get all the multi line comment from in the entire script?
      >> (yes I have red the documentation, but obviously this wasn't enough
      >> -) )[/color]
      >
      > What documentation did you read? AFAIK there is mention of a multiline
      > flag in the preg section. So all you do need to do is do a nongreedy
      > multiline match for /\/\*.*?/*///.
      >
      > FUP comp.lang.php
      >[/color]

      Thanks for the fast reply,

      I red the docs on the www.php.net

      I used it in the following code:

      $code=<<<end_of _code

      /**
      * comment 1
      */

      codeblock 1

      /**
      * comment 2
      */

      codeblock 2

      end_of_code;

      $pattern='/\/\*.*?/*///';

      if (preg_match($pa ttern,$code,$ar ray=array())){
      print_r($array) ;
      }

      I receive the following error:Warning: Unknown modifier '*' in
      What do i do wrong?

      Thanks Rob



      Comment

      • Daniel Tryba

        #4
        Re: Regular expression to get multi line comment

        Rob <reply@newsgrou p.nl> wrote:[color=blue]
        > I red the docs on the www.php.net[/color]

        All of them?
        [color=blue]
        > $pattern='/\/\*.*?/*///';
        >
        > if (preg_match($pa ttern,$code,$ar ray=array())){
        > print_r($array) ;
        > }
        >
        > I receive the following error:Warning: Unknown modifier '*' in
        > What do i do wrong?[/color]

        You copy and pasted my regexp :) It should be /\/\*.*?\*\//

        But still missing is multiline support:

        and

        Comment

        • Daedalus

          #5
          Re: Regular expression to get multi line comment

          Try this:

          $pattern = '/\/\*(.*)\*\//Us';
          preg_match_all( $pattern, $string, $result);

          $result[0] is an array containing every matches (including /* */) and
          $result[1] is another array containing every matches from the first captured
          parenthesized subpattern (in this case it only exclude /* */).
          Use:
          print_r($result );
          to view the results

          Dae

          "Rob" <reply@newsgrou p.nl> wrote in message
          news:d1rc5s$ch$ 1@reader10.wxs. nl...[color=blue]
          > Hello all,
          >
          > If I have the following code fragment:
          > /*
          > comment bla bla
          > */
          > ...code...
          >
          > With a regular expression, how do I get/extract the comment inside this
          > multi line comment block. With and without the comment characters?
          > And how do I get all the multi line comment from in the entire script?
          > (yes I have red the documentation, but obviously this wasn't enough ;-) )
          >
          >
          > Thanks in advance
          > Rob
          >
          >
          >
          >
          >
          >
          >
          >[/color]


          Comment

          • Rob

            #6
            Re: Regular expression to get multi line comment


            "Daniel Tryba" <partmapsswen@i nvalid.tryba.nl > schreef in bericht
            news:42414c64$0 $149$c5fe704e@n ews6.xs4all.nl. ..[color=blue]
            > Rob <reply@newsgrou p.nl> wrote:[color=green]
            >> I red the docs on the www.php.net[/color]
            >
            > All of them?
            >[color=green]
            >> $pattern='/\/\*.*?/*///';
            >>
            >> if (preg_match($pa ttern,$code,$ar ray=array())){
            >> print_r($array) ;
            >> }
            >>
            >> I receive the following error:Warning: Unknown modifier '*' in
            >> What do i do wrong?[/color]
            >
            > You copy and pasted my regexp :) It should be /\/\*.*?\*\//
            >
            > But still missing is multiline support:
            > http://nl2.php.net/manual/en/referen....modifiers.php
            > and
            > http://nl2.php.net/manual/en/functio...-match-all.php[/color]

            Thanks,

            Adding multiline support(?s) didnot do the trick as explained in the docs.
            But the following pattern did:

            $pattern='/\*[^*]*\*+([^/*][^*]*\*+)*/';

            Rob


            Comment

            Working...