gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com