删除评论ex。,“//”中的数据。或“/ * * /” [英] Remove data present in comments ex.,"//" or "/* */"

查看:111
本文介绍了删除评论ex。,“//”中的数据。或“/ * * /”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,任何人都可以帮助我,



i想要删除评论中的数据



例如:



// #define 0

// #define 1

/ * #define 2 * /

/ * #define 3

#define 4 * /

#define 5

/ * abcd < br $>


* /



// efgh







// #define 6

#define 7

/ * #define 8
#define 9

// #define 10



输出应为:

#定义5

#define 7

#define 9



i试试这个,



字符串行; string str;

StringBuilder sb = new StringBuilder();



//读取文件并逐行显示。

使用(System.IO.StreamReader file = new System.IO.StreamReader(FilePath))

{

while((line = file.ReadLine())!= null)

{

if(line.Contains(#define))

{< br $> b $ b

str = line;



sb.AppendLine(line.ToString());





}

}

}

str = sb .ToString();





string pattern = @(// [\t | \s | \w | \d |。\] * [\r\\\
| \\\
])|([\s | \t] * / \ * [\t | \s | \w | \ W | \d | \。| \ r | \ n] * \ * /)|(\< [!%] [\\\\] *( - ([^ \-] | [\\ n]] | - [^ \ - ])* - [\\\\\\%] *) \\>);





正则表达式rx =新正则表达式(pattern,RegexOptions.IgnoreCase);

MatchCollection mc = rx.Matches(str);



foreach(mc中匹配m)

{



str = str.Replace(m.Value,);

MessageBox.Show(str);

}



但是直到文件结束都无法读取,它读取到#define 10,有一行空,它停在那里,我想读取#之间的所有数据定义并删除评论的数据,谢谢

解决方案

http://stackoverflow.com/questions/9113163/remove-all-comment-single-multi-line-blank-lines-from-source-file [ ^ ]


Hallo can any one help me please,

i want to delete data present in Comments

Forexample:

//#define 0
//#define 1
/*#define 2*/
/*#define 3
#define 4*/
#define 5
/* abcd

*/

// efgh



// #define 6
#define 7
/* #define 8
#define 9
// #define 10

output should be:
#define 5
#define 7
#define 9

i tried thisway,

string line; string str;
StringBuilder sb = new StringBuilder();

// Read the file and display it line by line.
using (System.IO.StreamReader file = new System.IO.StreamReader("FilePath"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains("#define"))
{

str = line;

sb.AppendLine(line.ToString());


}
}
}
str = sb.ToString();


string pattern = @"(//[\t|\s|\w|\d|\.]*[\r\n|\n])|([\s|\t]*/\*[\t|\s|\w|\W|\d|\.|\r|\n]*\*/)|(\<[!%][ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t%]*)\>)";


Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection mc = rx.Matches(str);

foreach (Match m in mc)
{

str = str.Replace(m.Value, "");
MessageBox.Show(str);
}

but am not able to read all till file end, it reads upto #define 10 , there is a one line empty ,its stopping there, i want to read all data between #define and delete comment's data, thanks

解决方案

http://stackoverflow.com/questions/9113163/remove-all-comment-single-multi-line-blank-lines-from-source-file[^]


这篇关于删除评论ex。,“//”中的数据。或“/ * * /”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆