匹配所有三种PHP注释与REGEX [英] Matching all three kinds of PHP comments with REGEX

查看:144
本文介绍了匹配所有三种PHP注释与REGEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是REGEX新手,我需要一些帮助。

I'm new to REGEX and I need some help.

我需要匹配PHP可能有的三种类型的注释:

#单行注释

//单行注释 b $ b / *多行注释* /

I need to match all three types of comments that PHP might have:
# Single line comment
// Single line comment
/* Multi-line comments */

/**
 * And all of it's possible variations
 */

我应该提到的,我这样做是为了能够识别一个PHP结束标记(?> )是否在注释内,如果它是然后忽略它,如果不是那么使它计为一。这将被用在XML文档内部,以便改进Sublime Text对结束标记的识别(因为它驱动我坚果!)。我试图实现这个几个小时,但不能,所以如果你可以翻译为它与XML工作,我会很感激。 :)

Something I should mention, I am doing this in order to be able to recognize if a PHP closing tag (?>) is inside a comment or not, if it is then ignore it, if not then make it count as one. This is gonna be used inside an XML document in order to improve Sublime Text's recognition of the closing tag (cause it's driving me nuts!). I tried to achieve this a couple hours but wasn't able, so if you could translate for it to work with XML I would appreciate it. :)

所以,如果你也可以包括if-then-else登录,我会真的很感激。 BTW,我真的需要它是纯REGEX表达式,没有语言功能或任何东西。 :)

So if you could also include the if-then-else login I would really appreciate it. BTW, I really need it to be in pure REGEX expression, no language features or anything. :)

像Eicon提醒我,我需要所有的人都能够在行的开头或一段代码的结尾处匹配,所以我还需要以下所有的:

Like Eicon reminded me, I need all of them to be able to match at the start of the line, or at the end of a piece of code, so I also need the following with all of them:

<?php
echo 'something'; # this is a comment
?>

任何帮助将不胜感激。 :)

Any help would be appreciated. :)

推荐答案

解析编程语言对于正则表达式来说太多了。你应该可以寻找一个PHP解析器。

Parsing a programming language seems too much for regexes to do. You should probably look for a PHP parser.

但这些将是正在寻找的正则表达式。我假设所有的人,你使用DOTALL或SINGLELINE选项(虽然前两个会工作没有它):

But these would be the regexes you are looking for. I assume for all of them that you use the DOTALL or SINGLELINE option (although the first two would work without it as well):

~#[^\r\n]*~
~//[^\r\n]*~
~/\*.*?\*/~s

请注意,如果注释分隔字符出现在字符串或其他地方,它们实际上不会打开注释。

Note that any of these will cause problems, if the comment-delimiting characters appear in a string or somewhere else, where they do not actually open a comment.

您也可以将所有这些组合成一个regex:

You can also combine all of these into one regex:

~(?:#|//)[^\r\n]*|/\*.*?\*/~s

或不需要分隔符(如Java或C#)的语言,请删除那些。在这种情况下,您还必须以不同的方式应用DOTALL选项。但是如果不知道你要使用这个,我不能告诉你如何。

If you use some tool or language that does not require delimiters (like Java or C#), remove those ~. In this case you will also have to apply the DOTALL option differently. But without knowing where you are going to use this, I cannot tell you how.

如果你不能/不想设置DOTALL选项,这将是等价的我也省略了分隔符给一个例子):

If you cannot/do not want to set the DOTALL option, this would be equivalent (I also left out the delimiters to give an example):

(?:#|//)[^\r\n]*|/\*[\s\S]*?\*/

查看此处以进行工作演示。

现在,如果您还想捕获组中注释的内容,则可以这样做

Now if you also want to capture the contents of the comments in a group, then you could do this

(?|(?:#|//)([^\r\n]*)|/\*([\s\S]*?)\*/)

无论注释的类型如何,注释内容(无语法分隔符)

Regardless of the type of comment, the comments content (without the syntax delimiters) will be found in capture 1.

另一个工作演示

这篇关于匹配所有三种PHP注释与REGEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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