Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用于检查代码块内的行的正则表达式_Regex - Fatal编程技术网

Regex 用于检查代码块内的行的正则表达式

Regex 用于检查代码块内的行的正则表达式,regex,Regex,我有一个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服务启动脚本,如下所示:

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个正则表达式,只能在 1)
开始)
和 2)
重新启动)
代码块

我尝试了各种方法,例如:

^\s*start\)(?:(?:(?:#|[ \t]+)[^\n]*|)\n)*?^\s*haproxy_check
上面的匹配很好,但如果注释掉了
haproxy\u check
,则会出错

有什么建议吗

感谢和问候, Pravin Goyal

此正则表达式(您可以现场查看):

我来做这项工作

它确保:

  • 字符串以空格开头,后跟
    start
    restart
  • 由于
    *?
  • 该块包含
    haproxy\u检查
    (已注释或未注释
说明 特别说明:

我创造性地在
haproxy\u check
子字符串中添加了一个序列化的数字,这有助于说明我们正在捕获所需的实例。要删除此行为,只需删除
\uu0-9]
构造。这两个表达式之间的唯一区别在于前两个字符是我指定的
start
restart

表达式

\n\s*start\)(?:(?:(?![#"]|;;).)|\#[^\n]*\n|"[^"]*?")*?\K(haproxy_check_[0-9]+)
MATCH for the Start Block
1.  [56-72] `haproxy_check_02`

MATCH for the Restart block
1.  [563-579]   `haproxy_check_32`

此正则表达式将执行以下操作:

  • 分别在
    start)
    restart)
    块中找到
    haproxy\u检查
    子字符串,并将其放入捕获组0。它还包括在捕获组1中,以便在实时演示中突出显示。可以通过删除
    haproxy\u check
  • 仅直接从开始或重新启动块捕获
    haproxy\u检查
    ,不要意外地从后续代码块提取值
  • 将跳过注释掉的行
  • 忽略
    #
  • 当包含在双引号块中时,忽略haproxy\u检查
例子 现场演示

示例文本

我已经从中总结出示例文本,以包括我们正在寻找的内容和我们没有寻找的内容的示例。如上所述,为了便于说明,我添加了u和数字来序列化每个
haproxy\u检查

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
样本匹配

\n\s*start\)(?:(?:(?![#"]|;;).)|\#[^\n]*\n|"[^"]*?")*?\K(haproxy_check_[0-9]+)
MATCH for the Start Block
1.  [56-72] `haproxy_check_02`

MATCH for the Restart block
1.  [563-579]   `haproxy_check_32`
解释
您使用的是哪种编程语言?发布主题字符串的完整示例。从他的示例来看,这看起来像bash脚本。@RoYoMi讨论到哪里去了?你移除了吗?我困了,这让我无法解开正在发生的灾难性回溯。核心问题是,如果它被注释掉,它不应该匹配
haproxy\u check
。谢谢。这太棒了!有一个问题,我们可以只使用多行修饰符
m
?不幸的是,我正在使用的工具不支持
s
修饰符。我能得到的最接近的工具是
^\s*start\)(?:(?:#|[\t]+)[^\n]*.\n)*?\s+(haproxy\u检查)
。问题是,如果注释掉了
haproxy\u check
,它会流到其他代码块。如何限制它
?S选项只是告诉正则表达式引擎允许点匹配新行字符。因此,只需将表达式中的点替换为
(?:[^a]|[a])
(?:\s|\s)
(?:.|[\r\n])
,所有这些都会像
一样使用s选项。另请参见显示了带有其中一个修改的正则表达式,该正则表达式在没有S选项的情况下工作!非常感谢你的帮助。再次非常感谢!
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
----------------------------------------------------------------------