正则表达式匹配外部引号之间的所有内容(如果存在),否则整个文本blob由未转义空格分隔

问题描述 投票:1回答:1

虽然问题标题是关于Regex的,但我会接受我在问题正文中解释的问题的任何解决方案

语境:

我有一个脚本,一旦它对其中一个参数执行了一些操作,它就将所有参数($ @)传递给另一个脚本。详细信息超出了本问题的范围,但如果有必要,我很乐意在评论部分讨论它们。

我在找什么:

我想要的是能够修改我的正则表达式(见下文),这样我就不需要维护以下格式的白名单:... (?= command1| command2| command3) ... [编辑:]其中command*可以是任何单词

我希望能够包含传递给参数(-p, - project)的整个文本blob,包括引号(如果存在于新变量中)。

尝试:

我已经成功构建了一个解决我的直接问题的正则表达式,please view the explanation here。我认为它通过例子解释了我的问题。

我构建的正则表达式:

(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)

测试字符串:

pretend-cli -p /path\ to\ data/path/to/data01 command1 --some-other=123
pretend-cli -p "/path to data/path/to/data02" command2 --some-other=123
pretend-cli -p '/path to data/path/to/data03' command3 --some-other=123
pretend-cli -p=/path\ to\ data/path/to/data04 command1 --some-other=123
pretend-cli -p="/path to data/path/to/data05" command2 --some-other=123
pretend-cli -p='/path to data/path/to/data06' command3 --some-other=123
pretend-cli --project /path\ to\ data/path/to/data07 command1 --some-other=123
pretend-cli --project "/path to data/path/to/data08" command2 --some-other=123
pretend-cli --project '/path to data/path/to/data09' command3 --some-other=123
pretend-cli --project=/path\ to\ data/path/to/data10 command1 --some-other=123
pretend-cli --project="/path to data/path/to/data11" command2 --some-other=123
pretend-cli --project='/path to data/path/to/data12' command3 --some-other=123

但正如你所看到的,它需要我保持一个白名单。

进一步说明

它在我的脚本的当前实现中的样子(12个独立的测试用例):

PRETEND_PARAMETERS_01="-p /path\ to\ data/path/to/data01 command1 --some-other=123"
PRETEND_PARAMETERS_02="-p \"/path to data/path/to/data02\" command2 --some-other=123"
PRETEND_PARAMETERS_03="-p '/path to data/path/to/data03' command3 --some-other=123"
PRETEND_PARAMETERS_04="-p=/path\ to\ data/path/to/data04 command1 --some-other=123"
PRETEND_PARAMETERS_05="-p=\"/path to data/path/to/data05\" command2 --some-other=123"
PRETEND_PARAMETERS_06="-p='/path to data/path/to/data06' command3 --some-other=123"
PRETEND_PARAMETERS_07="--project /path\ to\ data/path/to/data07 command1 --some-other=123"
PRETEND_PARAMETERS_08="--project \"/path to data/path/to/data08\" command2 --some-other=123"
PRETEND_PARAMETERS_09="--project '/path to data/path/to/data09' command3 --some-other=123"
PRETEND_PARAMETERS_10="--project=/path\ to\ data/path/to/data10 command1 --some-other=123"
PRETEND_PARAMETERS_11="--project=\"/path to data/path/to/data11\" command2 --some-other=123"
PRETEND_PARAMETERS_12="--project='/path to data/path/to/data12' command3 --some-other=123"

我从这些参数中解析出“项目路径”的方式:

PRETEND_PROJECT_PATH_01=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_01})
PRETEND_PROJECT_PATH_02=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_02})
PRETEND_PROJECT_PATH_03=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_03})
PRETEND_PROJECT_PATH_04=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_04})
PRETEND_PROJECT_PATH_05=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_05})
PRETEND_PROJECT_PATH_06=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_06})
PRETEND_PROJECT_PATH_07=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_07})
PRETEND_PROJECT_PATH_08=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_08})
PRETEND_PROJECT_PATH_09=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_09})
PRETEND_PROJECT_PATH_10=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_10})
PRETEND_PROJECT_PATH_11=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_11})
PRETEND_PROJECT_PATH_12=$(grep -oP '(?:-p|--project)[= ]\K(.*)(?= command1| command2| command3)' <<< ${PRETEND_PARAMETERS_12})

要查看这些新变量的含义:

echo ${PRETEND_PROJECT_PATH_01}
echo ${PRETEND_PROJECT_PATH_02}
echo ${PRETEND_PROJECT_PATH_03}
echo ${PRETEND_PROJECT_PATH_04}
echo ${PRETEND_PROJECT_PATH_05}
echo ${PRETEND_PROJECT_PATH_06}
echo ${PRETEND_PROJECT_PATH_07}
echo ${PRETEND_PROJECT_PATH_08}
echo ${PRETEND_PROJECT_PATH_09}
echo ${PRETEND_PROJECT_PATH_10}
echo ${PRETEND_PROJECT_PATH_11}
echo ${PRETEND_PROJECT_PATH_12}
regex bash grep parameter-passing
1个回答
1
投票

Bash不支持您尝试使用的perl正则表达式功能。如果您需要坚持打击,请检查以下是否有帮助。

foo()
{
    echo "invoked with '$#' arguments: [$*]"
}

run_cli()
{
    path=
    # look for the value of -p/--project option
    for i in $(seq 1 $#); do
        if [[ "${!i}" =~ (-p|--project)(.*) ]]; then
            if [[ "${BASH_REMATCH[2]}" == '' ]]; then
                i=$(( i + 1 ))
                path="${!i}"
            else
                path="${BASH_REMATCH[2]:1}"
            fi
            break
        fi
    done
    echo "path: [$path]"   # do what you want with the path
    foo "$@"    # call the other script here with the original set of arguments
}

# Usage examples
echo "- eg 1"
run_cli -p /path\ to\ data/path/to/data01 command1 --some-other=123

echo "- eg 2"
run_cli -p /path\ to\ data/path/to/data01 --some-other=123

echo "- eg 3"
run_cli -p=/path\ to\ data/path/to/data01 --some-other=123

输出:

- eg 1
path: [/path to data/path/to/data01]
invoked with '4' arguments: [-p /path to data/path/to/data01 command1 --some-other=123]

- eg 2
path: [/path to data/path/to/data01]
invoked with '3' arguments: [-p /path to data/path/to/data01 --some-other=123]

- eg 3
path: [/path to data/path/to/data01]
invoked with '2' arguments: [-p=/path to data/path/to/data01 --some-other=123]
© www.soinside.com 2019 - 2024. All rights reserved.