正则表达式,用于匹配引号之间(包括引号)之间的所有内容,否则,整个文本blob均由未转义的空格隔开

时间:2019-04-18 07:32:22

标签: regex bash grep parameter-passing

尽管问题标题专门针对正则表达式,但我可以接受在问题正文中解释的所有问题的解决方案

上下文:

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

我正在寻找什么:

我想要的是能够修改我的正则表达式(见下文),这样我就不需要维护以下格式的白名单:... (?= command1| command2| command3) ... [edit:] 其中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}

1 个答案:

答案 0 :(得分:1)

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]

输出:

    var suggestions=[];

    function sendSuggestion() {
        if (document.getElementById("1").value == "no" && document.getElementById("2").value == "no"  ){
            suggestions.push("you need to study more");


        }


    }
</script>