为什么这个 Grep 表达式找不到有效的正则表达式?

Why is this Grep Expression not finding valid regex expression?

下面的正则表达式用于使用测试文件查找有效的 Amazon Cognito IdentityPool ID,但使用与 grep 相同的表达式找不到有效匹配项,但正则表达式与 https://regextester.com 上的测试字符串匹配 正则表达式:(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d:[0-9a-f-]+ 或什至简化为 [\w-]+:[0-9a-f-]+。 对于如下所示的测试字符串,两者均失败,但在 Regextester 上匹配。

us-west-1:de3e-e43-aeefe-bd
us-west-2:323-aaa33-a23d-dfe-daf

运行 grep 像: grep -oEarHn "(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d:[0-9a-f-]+" test.txt

您需要将正则表达式中的 \d\d 更改为 [0-9][[:digit:]]

grep id (iirc) 的默认模式POSIX 正则表达式。 \d 来自 PCRE。如果要启用 \d,可以将 -P 标志添加到 grep。这会启用类似 perl 的正则表达式,其中支持 \d。确保不能同时使用 -E-P 标志。