gpt4 book ai didi

python - 使用 python 从源文件中剥离 C/C++ 注释

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

我有一个忽略以/* ... */开头的多行注释的正则表达式但不适用于以//

开头的行

有人可以建议在此正则表达式中添加什么以使其忽略

pattern = r"""
## --------- COMMENT ---------
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *'s
( ##
[^/*][^*]*\*+ ##
)* ## 0-or-more things which don't start with /
## but do end with '*'
/ ## End of /* ... */ comment
| ## -OR- various things which aren't comments:
( ##
## ------ " ... " STRING ------
" ## Start of " ... " string
( ##
\\. ## Escaped char
| ## -OR-
[^"\\] ## Non "\ characters
)* ##
" ## End of " ... " string
| ## -OR-
##
## ------ ' ... ' STRING ------
' ## Start of ' ... ' string
( ##
\\. ## Escaped char
| ## -OR-
[^'\\] ## Non '\ characters
)* ##
' ## End of ' ... ' string
| ## -OR-
##
## ------ ANYTHING ELSE -------
. ## Anything other char
[^/"'\\]* ## Chars which doesn't start a comment, string
) ## or escape
"""

最佳答案

如果您计划使用当前的正则表达式,可以执行以下操作来匹配 //... 注释:

在此之下:

 /                ##  End of /* ... */ comment

添加这个:

 |                  ## OR it is a line comment with //
\s*//.* ## Single line comment

参见 demo

关于python - 使用 python 从源文件中剥离 C/C++ 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31840040/

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