更新时间:2022-11-15 08:27:03
正如评论中所指出的,expr
只使用basic";(又名过时")正则表达式,并且 bash 的 =~
运算符使用的正则表达式引擎不支持前瞻或后视(或 s
空格的简写)).但是你不需要环顾四周,只需匹配一切并使用捕获组来挑选你想要的部分(并将模式存储在一个变量中以避免可能的解析不一致):
As was pointed out in the comments, expr
only uses "basic" (aka "obsolete") regular expressions, and the regex engine that bash's =~
operator uses doesn't support lookahead or lookbehind (or the s
shorthand for space either). But you don't need lookaround, just match everything and use a capture group to pick out the part you want (and store the pattern in a variable to avoid possible parsing inconsistencies):
WorkingDirPattern='"WorkingDir":[[:space:]]"([^"]+)"'
if [[ "$config" =~ $WorkingDirPattern ]]; then
WorkingDir="${BASH_REMATCH[1]}" # get the contents of the first capture group
echo "WorkingDir is $WorkingDir"
else
echo "No WorkingDir found" >&2
fi