[英]bash regex difference between Linux and MacOS
以下腳本包含帶有反向引用的正則表達式:
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?
感謝@user1934428,我在 MacOS 上man 3 regex
的BUGS
部分發現了以下內容:
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.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.