首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

正则:怎么将注释下对应的代码取出来

2012-06-06 
正则:如何将注释下对应的代码取出来Python codes /*--第一个sql--*/if exists (select * from dbo.sy

正则:如何将注释下对应的代码取出来

Python code
s = '''/*--第一个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB1')  )drop table [dbo].[d_BB1]GO/*--第二个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB2')  )drop table [dbo].[d_BB2]GO/*--第三个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB3')  )drop table [dbo].[d_BB3]GO/*--第四个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB4')  )drop table [dbo].[d_BB4]GO'''findkey0 = ('/[*]--.*?--[*]/')findkey1 = ('/[*]--.*?--[*]/[\s\S]*?/[*]--.*?--[*]/')findkey2 = ('--[*]/[\s\S]*?/[*]--')rs =  re.findall(findkey1,s,re.I)for r in  rs:    print r



运行结果是
Python code
/*--第一个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB1')  )drop table [dbo].[d_BB1]GO/*--第二个sql  --*//*--第三个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB3')  )drop table [dbo].[d_BB3]GO/*--第四个sql  --*/



怎么把每个注释下的代码取出来,要包含对应的注释

[解决办法]
Python code
#!/usr/bin/env pythonimport res = '''/*--第一个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB1')  )drop table [dbo].[d_BB1]GO/*--第二个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB2')  )drop table [dbo].[d_BB2]GO/*--第三个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB3')  )drop table [dbo].[d_BB3]GO/*--第四个sql  --*/if exists (select * from dbo.sysobjects where id = object_id('d_BB4')  )drop table [dbo].[d_BB4]GO'''pat = re.compile(r'(/\*.*?)(?=/\*|$)', re.S | re.U)rs = pat.findall(s)if rs:    for line in rs:        print line 

热点排行