正则表达式可忽略注释行 [英] Regular Expression to ignore commented lines

查看:88
本文介绍了正则表达式可忽略注释行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建正则表达式来解析一段代码.

这是我的代码snipett

 Method1( 100 ,variable1)


  @"  


从上面的表达式中,我正在提取不同的数据.

但是在某些情况下,我有机会发表评论
如下所示

 Method1( 100 ,variable1)' 解决方案

"


从上面的表达式中,我正在提取不同的数据.

但是在某些情况下,我有机会发表评论
如下所示

 Method1( 100 ,variable1)'  字符串模式=  @"  ^ \ s * Method1 \ s * \(\ s *(\ d +)\ s *,\ s *([^)] *)\)(\ s * \'.*)?\ s * 

I am trying to create regular expression to parse a piece of code.

here is my code snipett

Method1(100,variable1)


@"^(?\Method1)\((?\d+),\s(?\w+)\)$"


from above expression i am extracting different data.

But i some cases there are chances to commented lines
like below

Method1(100,variable1) 'method1 is called here



so how what should i add in my above regular expression to ignore strings after the '')'' close bracket

解决方案

"


from above expression i am extracting different data.

But i some cases there are chances to commented lines
like below

Method1(100,variable1) 'method1 is called here



so how what should i add in my above regular expression to ignore strings after the '')'' close bracket


You can try the regular expression

''/\*([^*]|[\r\n]|(\*([^/]|[\r\n])))*\*/''

to ignore comments in the code


Here it is :

string pattern = @"^\s*Method1\s*\(\s*(\d+)\s*,\s*([^)]*)\)(\s*\'.*)?\s*


这篇关于正则表达式可忽略注释行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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