正则:如何将注释下对应的代码取出来
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
/*--第一个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 --*/
#!/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