Regex to find comment in c# source file

此生再无相见时 提交于 2019-12-11 12:45:15

问题


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?


回答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:

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



回答2:


Need multiline

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



回答3:


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)+)//\*+



回答4:


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

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



来源:https://stackoverflow.com/questions/5378662/regex-to-find-comment-in-c-sharp-source-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!