regex在perl中工作,在bash中无法匹配,在zsh中工作,但BASH_REMATCH为空

vatpfxk5  于 2022-12-01  发布在  Perl
关注(0)|答案(1)|浏览(167)
for d in "A - test1 (a)" "B - test2 (b)";
do
  if [[ "$d" =~ -\s(.*?)\s\( ]];
  then
    D="${BASH_REMATCH[1]}"
    echo "$d --> $D : $BASH_REMATCH"
  else
    echo "NO MATCH $d"
  fi
done

在bash中,此输出

NO MATCH A - test1 (a)
NO MATCH B - test2 (b)

在zsh中,它会失败

zsh: failed to compile regex: Unmatched ( or \(

如果我在zsh中将表达式修改为[[ "$d" =~ "-\s(.*?)\s\(" ]],它将匹配,但没有捕获

A - test1 (a) -->  : 
B - test2 (b) -->  :

具有相同regex表达式的Perl脚本可以正常工作

$x="A - test1 (a)";

if ($x =~ /-\s(.*?)\s\(/) {
   print "$x -> $1\n";
} else {
   print "No match: $x\n";
}

这将按预期输出A - test1 (a) -> test1
如何使正则表达式在bash和zsh中都能工作(按照本例从A - test1 (a)中提取test)?

wrrgggsh

wrrgggsh1#

对于bash,使用[[:space:]]而不是\s,并删除?

for d in "A - test1 (a)" "B - test2 (b)"; do
  if [[ "$d" =~ -[[:space:]]([^[:space:]]*)[[:space:]]\( ]]; then
    D="${BASH_REMATCH[1]}"
    echo "$d --> $D"
  else
    echo "NO MATCH $d"
  fi
done
A - test1 (a) --> test1
B - test2 (b) --> test2

对于zsh,它几乎是相同的,只是必须使用match而不是BASH_REMATCH,并将正则表达式的最后一个括号引起来:

for d in "A - test1 (a)" "B - test2 (b)"; do
  if [[ "$d" =~ -[[:space:]]([^[:space:]]*)[[:space:]]'\(' ]]; then
    D="${match[1]}"
    echo "$d --> $D"
  else
    echo "NO MATCH $d"
  fi
done
A - test1 (a) --> test1
B - test2 (b) --> test2

相关问题