正则表达式以任何一个开头查找评论;或#个字符

时间:2011-09-06 20:59:06

标签: regex

我的ini文件中包含注释但在一个ini文件中注释以

开头
; Enable the PHP scripting language engine under Apache.
engine = On
; Enable compatibility mode with Zend Engine 1 (PHP 4.x)
zend.ze1_compatibility_mode = Off
; Allow the <? tag.  Otherwise, only <?php and <script> tags are recognized.

和另一个

# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB

# The maximum amount of concurrent sessions the MySQL server will
# allow. One of these connections will be reserved for a user with
# SUPER privileges to allow the administrator to login even if the
# connection limit has been reached.
max_connections=255

目前发现第一个使用正则表达式的开头涉及使用

^(;).*$\n

我尝试使用

修改此内容
^[;#].*$\n 

找到两个评论中的任何一个,但是找不到任何一种类型的评论的正确的正则表达不起作用?

你的所有建议都非常有帮助。我需要做的另一件事是关闭应用程序,然后重新打开它以查看更改是否生效。

3 个答案:

答案 0 :(得分:8)

这应该有效,但您可以尝试更改语法:

^(;|#).*$

答案 1 :(得分:3)

^(;|#).*$\n

似乎在这里工作正常

答案 2 :(得分:1)

在一些正则表达式中,#表示内联注释(意味着它注释掉了正则表达式的一部分)。如果^(;).*$\n有效但^[;#].*$\n没有,我会尝试使用反斜杠转义#

我也考虑忽略前导空格:

/^\s*[;\#].*?$/m

请注意/m以激活多线模式。这个(与惰性.*?结合使用)是您想要使用的模式,以便将模式应用于每一行。我也认为你最后不想要\n,就像你的模式一样;在\n之后放置$没有任何意义,除非在多线模式下,除了防止最后一行匹配外,它基本上什么都不做。

让我们知道您正在使用哪种正则表达式,以及尝试使用您的版本时究竟发生了什么,这可能会有所帮助。