如果没有单独指定,bash正则表达式会找到特定的单词和字符

时间:2018-02-07 02:20:35

标签: regex bash

我尝试使用正则表达式来表示bash if语句。

if [[ "$@" =~ blah blah ]]; then … ; fi

`result expected:
-a              (not match: as standalone)
-auto           (not match: as standalone)
-lv             (not match: no "a" or "--auto")
-l --verbose    (not match: no "a" or "--auto")
-v --log        (not match: no "a" or "--auto")
-n --make-auto  (not match: no "a" or "--auto")
-g --auto-gen   (not match: no "a" or "--auto")
-a -l           (match: "-a" used with other)
-l -a -v        (match: "-a" used with other)
-l -s -a        (match: "-a" used with other)
-ls -a          (match: "-a" used with other)
-va -s          (match: "-a" used with other)
-lav            (match: a used with other)
-alva           (match: a used with other)
-l --auto       (match: "--auto" used with other)
-l   --auto     (match: "--auto" used with other)
--auto -lv      (match: "--auto" used with other)
--auto --log    (match: "--auto" used with other)
--auto   --log  (match: "--auto" used with other)`

我做了很多测试,我认为我对正则表达式一无所知 我已经阅读了很多教程和链接,但是如果我理解的话,那里没有AND但只有OR

`(^[^--auto]$|[[:space:]]--auto[[:space:]]{0,}|^--auto[[:space:]]{1,}[[:graph:]]{1,})
"--log --auto"              => OK 
"--log --auto --verbose"    => OK
"--log --auto-gen"          => KO match but need to not match no --auto`

`(^[^--auto]$|[[:space:]]--auto[[:space:]]{1,}|^--auto[[:space:]]{1,}[[:graph:]]{1,})
"--log --auto --verbose"    => OK
"--log --verbose --auto"    -> KO`

`(^[^--auto]$|[[:space:]]--auto[[:space:]]{1,}$|^--auto[[:space:]]{1,}[[:graph:]]{1,})`

依旧......

现在有了简短的参数......即使“-a”一个人也匹配任何东西!

`((^-([a-z0-9])*a{1,}.*)|([[:space:]]-([a-z0-9])*a{1,}.*))
"-a"                => KO match and do not need
"-l -a"             => OK
"-a -l"             => OK
"-la"               => OK
"-lv"               => OK
"-a -l -v"          => OK
"-v -z    -a"       => OK`

如果我在前面添加(^-[^a]$) ...没效果

`(^-[^a]$)|((^-([a-z0-9])*a{1,}.*)|([[:space:]]-([a-z0-9])*a{1,}.*))`

如果我混合短而长的badaboum!

我可能会通过分别测试params来解决这个问题,但我正在尝试测试整条线路。

是否可以在正则表达式和bash中执行此操作?

提前感谢您的建议和帮助。

0 个答案:

没有答案