Matching Multiline C++ style comments using Regex

regular expression for comments in lex
python multiline comment regex
multiline regex javascript
multiline comment flex
regex match multiple lines until
regular expression match
regex remove single line comments

OK, I know it's a recurring issue, however I can't seem to find one single working solution based solely on regex.

So, this is what I've come up with (actually basing it on the 'literal' description of multiline comments in the C# sharp grammar specification by ECMA).


However, as you can see it's not working...

Demo :

Any ideas? Is this even possible without doing all sorts of hacks? (Well, I mean other than the regex itself... lol)

P.S. If it is of any informational value, I'm currently developing a lexer/parser/interpreter using Lex/Bison/C/D and obviously multiline comments is a thing to be considered...

Here is the working regex for your provided sample from the




Multiline Comments, We use ‹ . *? › to match anything between the two delimiters of the comment. The option “dot matches line breaks” that most regex engines have allows this to span multiple lines. We need to use a lazy quantifier to make sure that the comment stops at the first */ after the /* , rather than at the last */ in the file. Vim can search for text that spans multiple lines. For example, the search /hello\_sworld finds "hello world" in a single line, and also finds "hello" ending one line, with "world" starting the next line. In a search, \s finds space or tab, while \_s finds newline or space or tab: an underscore adds a newline to any character class.

In case you need this for flex, which doesn't implement non-greedy matches, here is one way of writing the regex:


Alternative, not much easier on the eyes:


The / doesn't need to be quoted. But the stars do, and it seems more consistent. Yet another option is to quote the stars with backslashes, but I find that even harder to read.

If you did need this for flex/lex, you would have been better off putting an appropriate tag, such as flex-lexer.

Finding Comments in Source Code Using Regular Expressions , This regular expression will match them both along with anything in between: start_code(); /* First more_code(); /* * Another common multi-line comment style. Narciso Jaramillo on Find/Replace: Allow matching across newlines. We should also offer the option of doing either multiline or non-multiline searches in either Find or Find in Files (where "multiline" means that ^ and $ match the beginning/end of each line instead of the beginning/end of the entire document) Currently Find acts like multiline and Find in Files doesn't (

In C# I get the best performance with @"(?s:/\*((?!\*/).)*\*/)".

If you like to match all comments (including line comments) use @"(?>/(/[^\r\n]*|(?s:\*((?!\*/).)*\*/)))".

Multi-line comments, now internal definition, only for zip.c and unzip.h */ /* now internal definition, * only for zip.c and unzip.h */. Substitution. Expression Flags. ignore case (i) pattern = "^(\w+)\s(\d+)\r*$" Console.WriteLine("With multiline option:") For Each match As Match In Regex.Matches(input, pattern, RegexOptions.Multiline) scores.Add(CInt(match.Groups(2).Value), match.Groups(1).Value) Next ' List scores in descending order.

Here is the short answer


It'll start from /*, then .* reads char one by one and \s reads white spaces (including new lines) and finally ends at */. This is working for me using C#.

Matching multiline comments in regex, It highlights everything from comment 1 to comment 3 as a big comment, including code block 1 and code block 2 . How can I get it to match (* to  Play Matching Games @ We have over 100,000 games. Enter & play now!

Multi-line comments, now internal definition, only for zip.c and unzip.h */ /* now internal definition, * only for zip.c and unzip.h */. Substitution. Expression Flags. ignore case (i) RegexOptions.Multiline. This Regex option makes handling multiple lines easier. With it, we can apply a regular expression over an input string that has many lines.

Advanced RegEx, It contains a multi-line comment as well as data outside of it. var str = ` Hello, World. /*Goodbye, World.*/Hello again, World. ` Using Regex, how  ECE145A/ECE218A Impedance Matching Notes set #5 Page 13 Basis for distributed matching using transmission line segments: the equivalent circuit model of a short transmission line. L/2 L/2 C L C/ 2 C/ 2 Z0 , τ L = τ Z0 C = τ/ Z0 τ=A/vp Let’s approximate a shunt inductor with a transmission line section. L1 Z1, τ1 L1 = Z1τ1

C comments and regular expressions, The C programming language has two kinds of comments, ones with a start Let's suppose you need to match the original kind of C comments. Currently I am using this for getting the c multiline comments with lookahead. In multiline the pattern character [^] match the first character of the string and the beginning of each line (following immediately after the each newline). While expression small "w" is used to mark the space with characters.