Helpful Information
 
 
Category: Regex Programming
Those Tricky Stars!

Ok, I figured out the strings myself, but I don't understand why my solution doesn't work for comments...

I am trying to detect /* whatever */

Here is my regular expression...
/\*[^(\*/)]*\*/

... I would expect that regular expression to work, but it doesn't...

This regular expression behaves as expected, but not as desired:
/\*[^(/)]*\*/

Why won't the first regular expression match? What can I do to make it match?

That is because character classes always match a single character.
So the class [^(\*/)] will match any character except '(', '*', '/' or ')'. As you see, the "normal" meta characters like '(' and ')' don't group characters inside a character class as they do outside a character class. Also, the '*' does not need escaping inside a character class and it just matches the character '*'.

All of that said, you can match comments like this:


(?s)/\*((?!\*/).)*\*/










privacy (GDPR)