下面的脚本包含带有反向引用的regex:
pattern="(a)\1"; [[ aa =~ $pattern ]] && echo okay || echo notokay在Linux上:
$ uname -a
Linux #9-Ubuntu SMP Fri Apr 10 21:10:36 UTC 2020
$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
$ bash -c 'pattern="(a)\1"; [[ aa =~ $pattern ]] && echo okay || echo notokay'
okay
$在MacOS上:
$ uname -a
Darwin MacBook-Pro.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:54 PST 2021
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
$ /bin/bash -c 'pattern="(a)\1"; [[ aa =~ $pattern ]] && echo okay || echo notokay'
notokay
$ /usr/local/bin/bash --version
GNU bash, version 5.1.12(1)-release (x86_64-apple-darwin21.1.0)
$ /usr/local/bin/bash -c 'pattern="(a)\1"; [[ aa =~ $pattern ]] && echo okay || echo notokay'
notokay
$我如何才能找出反向引用为什么在Linux上工作,而在MacOS上不起作用呢?
发布于 2021-12-28 12:17:31
多亏了@user1934428,我在man 3 regex on MacOS的man 3 regex部分中发现了以下提到:
The back-reference code is subtle and doubts linger about its correctness in complex cases.
...
It is advised to avoid back-references whenever possible.
...
The standard's definition of back references is vague. For example, does ‘a\(\(b\)*\2\)*d’ match ‘abbbd’?
Until the standard is clarified, behavior in such cases should not be relied on.https://stackoverflow.com/questions/70505338
复制相似问题