我有一个haproxy服务启动脚本,如下所示:
case "$1" in
start)
echo -n "Starting haproxy "
## Start daemon with startproc(8). If this fails
## the return value is set appropriately by startproc.
haproxy_check
/sbin/startproc $HAPROXY_BIN -D -f $HAPROXY_CONF -p $HAPROXY_PID
# Remember status and be verbose
rc_status -v
;;
stop)
echo -n "Shutting down haproxy "
## Stop daemon with killproc(8) and if this fails
## killproc sets the return value according to LSB.
/sbin/killproc -TERM $HAPROXY_BIN
# this is additional forcing kill command to ensure that all processes are stopped.
/usr/bin/killall -9 $HAPROXY_BIN || true
# Remember status and be verbose
rc_status -v
;;
try-restart|condrestart)
## Do a restart only if the service was active before.
## Note: try-restart is now part of LSB (as of 1.9).
## RH has a similar command named condrestart.
if test "$1" = "condrestart"; then
echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
fi
$0 status
if test $? = 0; then
# we us reload here for a graceful restart during update
$0 reload
else
rc_reset # Not running is not a failure.
fi
# Remember status and be quiet
rc_status
;;
restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
$0 start
haproxy_check
# Remember status and be quiet
rc_status
;;
check)
## Stop the service and regardless of whether it was
## running or not, start it again.
echo -n "Checking config of haproxy "
haproxy_check
rc_status -v
;;
reload|force-reload)
## Like force-reload, but if daemon does not support
## signaling, do nothing (!)
haproxy_check
# If it supports signaling:
echo -n "Reload service haproxy "
$HAPROXY_BIN -p $HAPROXY_PID -D -f $HAPROXY_CONF -sf $(cat $HAPROXY_PID)
rc_status -v
;;
status)
echo -n "Checking for service haproxy "
## Check status with checkproc(8), if process is running
## checkproc will return with exit status 0.
# Return value is slightly different for the status command:
# 0 - service up and running
# 1 - service dead, but /var/run/ pid file exists
# 2 - service dead, but /var/lock/ lock file exists
# 3 - service not running (unused)
# 4 - service status unknown :-(
# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
# NOTE: checkproc returns LSB compliant status values.
/sbin/checkproc $HAPROXY_BIN
# NOTE: rc_status knows that we called this init script with
# "status" option and adapts its messages accordingly.
rc_status -v
;;
probe)
## Optional: Probe for the necessity of a reload, print out the
## argument to this init script which is required for a reload.
## Note: probe is not (yet) part of LSB (as of 1.9)
test $HAPROXY_CONF -nt $HAPROXY_PID && echo reload
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
exit 1
;;
esac
我需要2个只能匹配haproxy_check
行的正则表达式
1)start)
和
2)restart)
代码块。
我尝试了各种各样的事情,例如:
^\s*start\)(?:(?:(?:#|[ \t]+)[^\n]*|)\n)*?^\s*haproxy_check
上述内容匹配正常,但如果haproxy_check
被注释掉,则会出错。
有什么建议吗?
谢谢和问候, Pravin Goyal
答案 0 :(得分:1)
特别提示:
我已经采取创造性自由在haproxy_check
子字符串中包含序列号,这有助于说明我们正在捕获所需的实例。要删除此行为,只需删除_[0-9]
构造。这两个表达式之间的唯一区别是前几个字符是我指定start
或restart
。
表达式
\n\s*start\)(?:(?:(?![#"]|;;).)|\#[^\n]*\n|"[^"]*?")*?\K(haproxy_check_[0-9]+)
\n\s*restart\)(?:(?:(?![#"]|;;).)|\#[^\n]*\n|"[^"]*?")*?\K(haproxy_check_[0-9]+)
此正则表达式将执行以下操作:
haproxy_check
或start)
块中找到restart)
子字符串并将其放入捕获组0.它也包含在捕获组1中,以便突出显示在现场演示。删除haproxy_check
haproxy_check
或直接重启块,不要意外从尾随代码块中提取值。#
haproxy_check
现场演示
https://regex101.com/r/yU3lK9/1
示例文字
我已经简化了示例文本,包括我们正在寻找的内容以及我们不寻找的内容示例。如上所述,为了便于说明,我添加了_和数字来序列化每个haproxy_check
case "$1" in
start)
# haproxy_check_01 ;;
haproxy_check_02
# haproxy_check_03
rc_status -v # haproxy_check_04
echo " haproxy_check_05 "
;;
stop)
# haproxy_check_11
haproxy_check_12
# haproxy_check_13
rc_status -v # haproxy_check_14
echo " haproxy_check_15 "
;;
try-restart|condrestart)
# haproxy_check_21
haproxy_check_22
# haproxy_check_23
rc_status -v # haproxy_check_24
echo " haproxy_check_25 "
;;
restart)
# haproxy_check_31
haproxy_check_32
# haproxy_check_33
rc_status -v # haproxy_check_34
echo " haproxy_check_35 "
;;
check)
# haproxy_check_41
haproxy_check_42
# haproxy_check_43
rc_status -v # haproxy_check_44
echo " haproxy_check_45 "
;;
reload|force-reload)
# haproxy_check_51
haproxy_check_52
# haproxy_check_53
rc_status -v # haproxy_check_54
echo " haproxy_check_55 "
;;
status)
# haproxy_check_61
haproxy_check_62
# haproxy_check_63
rc_status -v # haproxy_check_64
echo " haproxy_check_65 "
;;
probe)
# haproxy_check_71
haproxy_check_72
# haproxy_check_73
rc_status -v # haproxy_check_74
echo " haproxy_check_75 "
;;
*)
# haproxy_check_81
haproxy_check_82
# haproxy_check_83
rc_status -v # haproxy_check_84
echo " haproxy_check_85 "
;;
esac
样本匹配
MATCH for the Start Block
1. [56-72] `haproxy_check_02`
MATCH for the Restart block
1. [563-579] `haproxy_check_32`
NODE EXPLANATION
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
start 'start'
----------------------------------------------------------------------
\) ')'
----------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the least amount possible)):
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
[#"] any character of: '#', '"'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
;; ';;'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\# '#'
----------------------------------------------------------------------
[^\n]* any character except: '\n' (newline) (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
[^"]*? any character except: '"' (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
" '"'
----------------------------------------------------------------------
)*? end of grouping
----------------------------------------------------------------------
\K 'K'
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
haproxy_check_ 'haproxy_check_'
----------------------------------------------------------------------
[0-9]+ any character of: '0' to '9' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
答案 1 :(得分:0)
这个正则表达式(你可以看到它here):
/^\s+(?:re)?start.*?(haproxy_check).*?;;/gsm
会做的工作。
确保:
start
或restart
.*?
haproxy_check
(已注释或不)