正则表达式捕获 conf 文件的注释行 [英] regex to catch commented lines of a conf file

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

问题描述

为了在 conf 文件中找到这一行 pm.max_children = 50 并将其更改为 pm.max_children = 5,我使用了这个:

In order to find this line pm.max_children = 50 inside a conf file and change it to pm.max_children = 5, I am using this:

s/^\(pm.max_children = \).*/\15/

在这种情况下,该行可能会或不会被注释(使用;"或#").如何在单个正则表达式中处理该问题以使用 sed?

In the case, the line may or not be commented (with ';' or '#'). How can I deal with that in a single regex to work with sed?

如果 CONF_FILE 有这个内容:

If the CONF_FILE has this content:

pm.max_children = 500
;pm.max_children = 500
#pm.max_children = 500

这是我需要完成的:

pm.max_children = 5
pm.max_children = 5
pm.max_children = 5

推荐答案

sed 在我的机器上似乎不支持 ?,但您可以使用 * 代替:

sed on my machine doesn't seem to support ?, but you can use * instead:

s/^[#;]*[:space:]*\(pm.max_children = \).*/\15/

这匹配 0 个或多个 #; 字符后跟 0 个或多个空白字符.

This matches 0 or more # or ; characters followed by 0 or more whitespace characters.

如果您不关心关键字前面的内容,请使用它,它更简单,但可以匹配任何内容:

If you don't care what precedes your keyword, use this, which is simpler, but matches anything:

s/^.*\(pm.max_children = \).*/\15/

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

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