user669550
user669550

Reputation: 1

Regex to find comment in c# source file

I can't get my regex to match the header of a c# code file. I basically want need to return the header if it exists.

Example:

#define debug
//****************************************************************************************************
//  <copyright file="" company="">
//      Copyright (c) . All rights reserved.
//  </copyright>
//  <project>Engine</project>
//****************************************************************************************************

code here

//some other comment here

more code here

//another comment here

My regex looks like this:

(?:/\\*(?:[^*]|(?:\\*\+[^*/]))*\\*\+/)|(?://.*)

but it only matches this line: //**********************************************************

and not the rest of the comment.

Comments can also end like this "*/".

whats wrong with my regex? why doesn't it catch the whole block?

Upvotes: 1

Views: 5229

Answers (4)

frank tan
frank tan

Reputation: 141

Using the regex pattern: (/*([^]|[\r\n]|(*+([^/]|[\r\n])))*+/)|(//.)

see more https://code.msdn.microsoft.com/How-to-find-code-comments-9d1f7a29/

Upvotes: 0

Mario
Mario

Reputation: 36487

I guess you'd like to extract the pseudo-xml code so the following expression should work. Note that you'll still have to remove the leading "//" in each line.

//\*+\n((?://.*\n)+)//\*+

Upvotes: 0

Stephen Chung
Stephen Chung

Reputation: 14605

Need multiline

(^\/\/.*?$|\/\*.*?\*\/)

Upvotes: 1

Josh M.
Josh M.

Reputation: 27783

Try this one - and you can pull out the entire comment (with the "//" or the group within to get just the text. This will return a match for each line. Please use the "Multiline" option to run this:

^/[/|*](.+)$

Upvotes: 1

Related Questions