re = /\(\*(?:(?!\(\*|\*\)).)*?\*\)/
str = '1. (*comment*)
some code that should not be matched
2. (****comment****)
some code that should not be matched
3. (*comment (*comment inside comment*) comment*)
some code that should not be matched
4. (**) some code that should not be matched (***)
5. (*multi-
line-
comment*) some code that should not be matched
6. (*comment (with parentheses)*)
some code that should not be matched
7. some code that (*comment*) should (not) be matched (*comment*)
'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html