-3

I am trying to write a regex that matches a comment that begins with a (* and ends at the first occurence of a *)

(* comment *)

From other posts on stack overflow about matching between parenthesis I put together the following regex.

\(\*(([^*\)])*)\*\)

This works as long as there are no * or ) characters within the comment. However, I want to allow these characters in a comment as long as they are not next to each other.

The following are all valid comments.

(*****)

(*()*)

(*)(*()**)

And a notable invalid comment is

(*)

I tried some stuff using lookaheads but to no avail. A pointer in the right direction would be appreciated.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
drew.neely
  • 126
  • 1
  • 13

1 Answers1

0

Try with this

match any string which contain an open parentheses followed by any characters any numbers of times followed by a closing parenthesis

\(\*.+\*\)
Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31
  • But `(**)` is a valid (but empty) comment too, so why not just `\(\*(.*)\*\)`? The `(.*)` part captures the content of the comment. – Rudy Velthuis Sep 24 '18 at 09:31