【发布时间】:2015-11-24 18:06:51
【问题描述】:
我正在使用 Python Regex,以清理为经典 ASP 页面生成的代码。
我需要删除单行或多行 ASP 评论块。 (ASP 注释行通常以 quote 开头)。
我的目标是匹配不包含可执行代码的块,而只匹配包含 cmets 的块。 cmets中是否有制表符或空格,我需要将这3个字符串替换为任何内容:
字符串 1:
<%' This multiline comment starts with two TAB characters after the quote
'and continues here
%>
字符串 2:
<% 'This multiline comment starts with SPACES characters before the quote
'and continues here, with TABS before the quote
' and with spaces before and after the quote
%>
字符串 3:
<%'This single line comment should at least be easy to remove%>
我尝试了以下正则表达式,但只取得了部分成功……:-/
output = re.sub(r'(<%(.*?)\')(.*?)(%>)', r'', output)
output = re.sub(r'<%(\t*|\s*)\'(.*)(%>)', r'', output)
你能给我一点建议吗? 非常感谢您的帮助:任何提示将不胜感激;-)
【问题讨论】:
-
也许一个简单的
<%[\S\s]*?%>就是如何解析它。注释样式标记通常是第一个分隔符到下一个分隔符。 -
感谢 sln :) 您的正则表达式可以工作,但可以针对任何 ASP 代码块,包括那些包含有用代码的代码块。我认为采用的策略是匹配每一行:1.- 以 \n (newline) 开头,2.- 后跟空格或制表符,3.- 然后加上引号。最终可以通过三个连续的正则表达式来完成,以便“更容易”找到引用(我的圣杯!)不幸的是,我无法成功制作正确的正则表达式字符串..哎哟!
标签: python regex asp-classic sublimetext2 sublime-text-plugin