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

标签 python regex asp-classic sublimetext2 sublime-text-plugin

我正在使用 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* 
 %>

关于python - 如何删除 Python 中的 ASP 仅注释 block (在 Sublime Text 2 上)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33900793/

相关文章:

session - 为什么 session 放弃不起作用?

python - 如何使用函数作为 while 循环条件?

php正则表达式获取href标签内的字符串

regex - 字段的正则表达式包含13位数字?

javascript - 将 javascript 变量保存在服务器端变量中(vbscript)

if-statement - 如何在 VBScript for Classic-ASP 中执行单行 If 语句?

python - 根据 Python 中另外两个数组的值创建数组的子集

python - 检查字典中键是否存在比在 Python 中捕获 KeyError 更好?

python - Pandas TimeGrouper : . median() 的行为与 .quantile(0.5) 不同

php - 使用正则表达式验证类/方法名称