From: Rod Knowlton Date: 2006-09-14T08:38:12+09:00 Subject: Re: Regular expression question. On Sep 13, 2006, at 10:55 AM, L7 wrote: > In trying to parse a C source file I have the following section of > code: > > ... > ... > case line > when /^.*\/\*.*?\*\/.*$/ # single line comment(s) > non_comments = line.split(/\/\*.*?\*\//).to_s > process_code(non_comments) > when /^.*\/\*\*?[^(\*\/)]*$/ # multi-line start > comment = true > next > when /^[^(\/\*)]*\*\/.*$/ # multi-line end > comment = false > ... > ... > > Is there a way to look for the pattern '*/' without having a single > '*' > break the search? If I'm not mistaken, what you need is a negative lookahead try /^.*\/\*([^\/]|\/(?!\*))*$/ for multi-line start and /^([^\*]|\*(?!\/))*\*\/.*$/ for multi-line end the key difference (from the start pattern) is ([^\/]|\/(?!\*)) this breaks down like so: ( [^\/] # anything but / | # or \/(?!\*) # a / not followed by an * (don't eat the character after /, just peek at it) ) The pattern for multi-line end uses the same technique, but with the characters reversed. I'm sure this isn't the be all and end all of C comment matching regexs, but it handles all of the cases you described. - Rod