开发者

Regex to find comments starting with either ; or # characters

开发者 https://www.devze.com 2023-04-03 15:35 出处:网络
I have ini files that have comments in thebut in one ini file the comments start with ; Enable the PHP scripting language engine under Apache.

I have ini files that have comments in the but in one ini file the comments start with

; 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.

and the other with

# 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

currently find the beginning of the first using regex involves using

^(;).*$\n

I tried to modify this using

^[;#].*$\n 

to find either of the two comments but it did n开发者_StackOverflow社区ot work what would be the correct regex to find comments of either type?

All of your suggestion where very helpful. The additional thing that I need to do was close the application and then reopen it to see the change take effect.


That should work, however you can try the alteration syntax:

^(;|#).*$


^(;|#).*$\n

Seems to work fine here


In some regex flavors, # denotes an inline comment (meaning that it comments out part of the regex). If ^(;).*$\n works but ^[;#].*$\n doesn't, I would try escaping the # with a backslash.

I'd consider ignoring leading whitespace as well:

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

Note the /m to activate multiline mode. This (combined with the lazy .*?) is what you want to use in order to apply the pattern to each line. I also don't think you want \n at the end, as in your pattern; putting \n after $ makes no sense except in multiline mode, where it will essentially do nothing except prevent a match on the final line.

It might help to let us know which regex flavor you're using, and what exactly happened when you tried your version.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号