Python 3正则表达式查找多行注释 [英] Python 3 regular expression to find multiline comment

查看:229
本文介绍了Python 3正则表达式查找多行注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Python 3中使用正则表达式查找PHP源代码中的注释块。PHP注释格式如下:

I'm trying to find comment blocks in PHP source code using regular expressions in Python 3. The PHP comments are in this format:

/**
 * This is a very short block comment
 */


$ b b

现在我想出了以下正则表达式:

Now I came up with the following regular expression:

'/\*\*[.]+?\*/'



我认为 - 结合DOTALL标志 - 但不是。它没有找到什么。奇怪的是,当我删除尾部斜杠,像这样:

I figure that -in combination with the DOTALL flag- should do it, but no. It doesn't find anything. Strange thing is that when I remove the trailing slash, like this:

'/\*\*[.]+?\*'

那么它会找到以下字符串:

then it finds the following string:

/**\n\t*


$ b b

我不知道为什么正则表达式不能找到一个星号后面跟着一个斜线...我检查了文件,我正在搜索双检查我没有在打印错误的评论(我didn' t)。
正则表达式中的斜杠也不是特殊字符,所以我不必逃避它。 (我试过,但它没有帮助。)

I have no idea why the regex can't find an asterisk followed by a slash... I checked the file that I'm searching to double check I didn't have a typo in the comment (I didn't). Also a slash is no special character in regex, so I wouldn't have to escape it. (I tried, but it didn't help.)

任何人都可以告诉我我的正则表达式有什么问题? :)

Can anyone tell me what's wrong with my regex? :)

顺便说一句,我也遇到了这个!线程,有人试图在Java做同样的。最后的胜利答案完成了他的正则表达式与我现在做的一样,所以我很无能的:(这可能是一个Python正则表达式的错误或我完全错过了一些东西?

By the way, I also came across this! thread where someone tried to do the same in Java. The final winning answer finished his regular expression the same way I do now, so I'm clueless :( Could this be a bug in Python regex or am I completely missing something?

任何帮助是非常感谢!:D

Any help is much appreciated! :D

推荐答案

可以使用 re.DOTALL 。 c>。 c>。

You can use the re.DOTALL flag to make the . character match newlines:

re.compile(r'/\*\*.+?\*/', re.DOTALL)


b $ b

(注意,PHP块注释可以以 / * 开头,而不仅仅是 / ** 。)

(As a side note, PHP block comments can start with /*, not just /**.)

这篇关于Python 3正则表达式查找多行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆