Which is a regex expression for identifying comments (i.e. between / * and * / across a few lines)

What is a regular expression expression for identifying comments (i.e. all characters between / * and * /, including these comment markers, and across multiple lines)?

So, for example, for a pickup truck:

/* asdf asdf asdf asdfasdfasdfasd asdfasdf */ 
+1
source share
3 answers
 (?:/\*(?:(?:[^*]|\*(?!/))*)\*/) 

This was originally part of the MySQL parser, designed to split comments without removing them from the lines:

 ("(?:(?:(?:\\.)|[^"\\\r\n])*)"|'(?:(?:(?:\\.)|[^'\\\r\n])*)'|`(?:(?:(?:\\.)|[^`\\\r\n])*)`)|((?:-- .*)|(?:#.*)|(?:/\*(?:(?:[^*]|\*(?!/))*)\*/)) 

This is replaced by capture group 1 to return the rows.

+2
source

This is a very difficult problem to solve with regex (since it is very difficult to consider all cases of edges). If this is a programming language that you are parsing, I would strongly suggest using a parser designed to parse this language.

+2
source

It is not so simple, for example:

 /* multiline comment f("end marker inside literal string */"); */ 

See How to use regex to erase C style comments from a file? .

+2
source

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


All Articles