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?

2
  • 2
    Have you tried using RegexOptions.MultiLine? Commented Mar 21, 2011 at 14:02
  • I am using MultiLine, but it only returns 1 line and that's the first. I am coding in C# Commented Mar 21, 2011 at 14:10

5 Answers 5

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:

^/[/|*](.+)$
Sign up to request clarification or add additional context in comments.

4 Comments

Sure, it's unclear if the asker whats the // or /* or */ part of the comment or just the "body."
This also works, and again I didn't think it would return an array, but just a single block with the whole comment.
I want the whole comment and not just the body:)
This does return the whole comment - one line at a time. Instead of grabbing the value from the group, just grab the entire match value.
1

Need multiline

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

3 Comments

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
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.
Now if you only want a block bracketed by stars (on the first and last line only): (^//\*+?$(?:^//.*?$)^//\*+$)
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)+)//\*+

Comments

0

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

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

Comments

0

If you have multiline comments using /*.... */ then following will work

\/\*[\s\S]*?\*\/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.