Skip to main content
 首页 » 编程设计

python之如何删除 Python 中的 ASP 仅注释 block (在 Sublime Text 2 上)

2025年05月04日15tuyile006

我正在使用 Python Regex,以清理为经典 ASP 页面生成的代码。

我需要删除单行或多行 ASP 注释 block 。 (ASP 注释行通常以 quote 开头)。

我的目标是匹配不包含可执行代码的 block ,但只匹配包含注释的 block 。 如果评论中有制表符或空格,我需要将这 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) 

你能给我一点建议吗? 非常感谢您的帮助:任何提示将不胜感激;-)

请您参考如下方法:

重新开始。
假设:

如果该行以单引号开头,则为注释。
获取所有包含仅 引用行的 block 。
点. metachar 不匹配换行符。

<%(?:\s*'.*)+\s*%>

格式化

 <% 
 (?: \s* ' .* )+ 
 \s*  
 %> 

匹配您的所有样本。

编辑

但为了安全起见,您应该在该点之前使用否定断言。

<%(?:\s*'(?:(?!%>).)*)+\s*%>

格式化

 <% 
 (?: 
      \s* '  
      (?: 
           (?! %> ) 
           .  
      )* 
 )+ 
 \s*  
 %>