如何在 ack 输出中使用命名正则表达式组?
假设我有一个包含以下内容的 foo.txt 文件:
[2010-11-13 12:00:02,656]
[2010-11-13 12:00:02,701]
[2010-11-13 12:00:02,902]
当我 ack
包含以下内容的日期部分时,它可以工作:
ack "(?P<foo>\d{4}-\d{2}-\d{2})" foo.txt --output "\$1"
2010-11-13
2010-11-13
2010-11-13
但是当我尝试使用 --output
时对于命名组“foo”,我无法让它工作:
ack "(?P<foo>\d{4}-\d{2}-\d{2})" foo.txt --output "(?P=foo)"
(?=foo)
(?=foo)
(?=foo)
非常感谢任何帮助。非常感谢。
Suppose I have a foo.txt file with the following content:
[2010-11-13 12:00:02,656]
[2010-11-13 12:00:02,701]
[2010-11-13 12:00:02,902]
When I ack
for the date portion with the following, it works:
ack "(?P<foo>\d{4}-\d{2}-\d{2})" foo.txt --output "\$1"
2010-11-13
2010-11-13
2010-11-13
But when I try to use --output
with the named group "foo", I cannot get it to work:
ack "(?P<foo>\d{4}-\d{2}-\d{2})" foo.txt --output "(?P=foo)"
(?=foo)
(?=foo)
(?=foo)
Any help is greatly appreciate it. Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(?P=foo)
仅在正则表达式内部起作用(它的命名等效于\1
)。$+{foo}
是$1
的命名等效项。此外,对于大多数 shell,单引号将帮助您避免额外的反斜杠:
(?P=foo)
only works inside the regular expression (it's the named equivalent of\1
).$+{foo}
is the named equivalent of$1
.Also, with most shells, single quotes will help you avoid extra backslashes: