1

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?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
user669550
  • 1
  • 1
  • 4

4 Answers4

1

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:

^/[/|*](.+)$
Josh M.
  • 26,437
  • 24
  • 119
  • 200
1

Need multiline

(^\/\/.*?$|\/\*.*?\*\/)
Stephen Chung
  • 14,497
  • 1
  • 35
  • 48
  • This works, and actually my own example does also. I see that it returns and array with the resut, but I was just expecting a single string with the whole block:x – user669550 Mar 21 '11 at 14:16
  • Mine is a generic one for matching comments. If you just want to match your exact format (notice that it really is a bunch of //'s together), you can try: `(^//\*.*?$)+` (multiline). This will match all lines prefixed with `//*` in a single block. Sorry I wasn't clear with what you want before. – Stephen Chung Mar 22 '11 at 03:50
  • Now if you only want a block bracketed by stars (on the first and last line only): `(^//\*+?$(?:^//.*?$)^//\*+$)` – Stephen Chung Mar 22 '11 at 03:53
0

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

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

frank tan
  • 131
  • 1
  • 4
0

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)+)//\*+
Mario
  • 35,726
  • 5
  • 62
  • 78