[Tutor] Re: Finding C comments with regular expressions

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Apr 21 15:07:34 EDT 2004



> The regular expression is a little tricky, because the beginning and
> ending of a comment uses two characters instead of one.  Here's a regular
> expression that takes this into consideration:
>
> ###
> pattern = re.compile(r"""
>            / \*                ##  Leading "/*"
>
>            (                   ##  Followed by any number of
>               ([^*])           ##  non star characters
>               |                ##  or
>               (\* [^/])        ##  star-nonslash
>            )*
>            \* /                ##  with a trailing "/*"
>           """, re.VERBOSE)
> ###


Argh.  The comment at the end of the regex is wrong.  Let me correct that:

###
pattern = re.compile(r"""
           / \*                ##  Leading "/*"

           (                   ##  Followed by any number of
              ([^*])           ##  non star characters
              |                ##  or
              (\* [^/])        ##  star-nonslash
           )*
           \* /                ##  with a trailing "*/"
          """, re.VERBOSE)
###


I'm being too hasty these days... *grin*  My apologies!




More information about the Tutor mailing list