Explain a regex that finds CSS comments

I found this regex code that finds comments on the w3.org CSS gramma page .

\/\*[^*]*\*+([^/*][^*]*\*+)*\/ 

It's quite a long time and a little hard to understand. I just put

 \/\*.*\*\/ 

to find comments, but when I tested it in RegexPal , it finds single-line comments, not multi-line comments, while the original regular expression can find all types of comments.

I dont understand what

 +([^/*][^*]*\*+)* 

part inside the original regular expression. Can someone explain this to me?

+6
source share
2 answers

The reason your only one-line comments are because of the typical regular expressions . matches anything other than newlines; while the other uses a negative character class that matches any other than the specified characters, and therefore can match newline characters.

However, if you fix it (usually there is an option for multiline or “as if one line” match), you will find that it will match /* first comment to */ last comment; will you need to use a non-greedy quantifier,. .*? to match no more than one comment.

However, the more complex regular expression you give is even more complex. Based on nikc.org's answer, I believe that it should provide a restriction that “comments may not be nested”; that is, they should not contain /* inside them. In other languages ​​that allow you to comment on /* like /* this */ (i.e., Inner / * is not forbidden or a nested comment), the template \/\*.*?\*\/ Would be appropriate to match them.

+6
source

Token using token:

 \/ <- an escaped '/', matches '/' \* <- an escaped '*', matches '*' [^*]* <- a negated character class with quantifier, matches anything but '*' zero or more times \*+ <- an escaped '*' with quantifier, matches '*' once or more ( <- beginning of group [^/*] <- negated character class, matches anything but '/' or '*' once [^*]* <- negated character class with quantifier, matches anything but '*' zero or more times \*+ <- escaped '*' with quantifier, matches '*' once or more )* <- end of group with quantifier, matches group zero or more times \/ <- an escaped '/', matches '/' 

Language Reference

Analysis at Regexper.com

+15
source

Source: https://habr.com/ru/post/908707/


All Articles