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



下面的正则表达式用于在测试文件中查找有效的Amazon Cognito IdentityPool ID,但在grep中使用相同的表达式找不到有效的匹配项,但正则表达式与上的测试字符串匹配https://regextester.comRegex表达式:(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 regex的默认模式。CCD_ 8来自PCRE。如果要启用d,可以将-P标志添加到grep中。这启用了类似perl的regex,其中支持d。请确保不能同时使用-E-P标志。

最新更新