如何在ack输出中使用命名的正则表达式组? [英] How to use named regex groups in ack output?

查看:93
本文介绍了如何在ack输出中使用命名的正则表达式组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含以下内容的foo.txt文件:

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]

当我对日期部分进行 ack 时,它起作用:

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

但是当我尝试将-output 与命名组 foo一起使用时,我无法获取工作:

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.

推荐答案

ack "(?P<foo>\d{4}-\d{2}-\d{2})" foo.txt --output "\$+{foo}"

(?P = foo)仅在正则表达式内 起作用(这是等价于 \1 )。 $ + {foo} $ 1 的等效名称。

(?P=foo) only works inside the regular expression (it's the named equivalent of \1). $+{foo} is the named equivalent of $1.

对于大多数shell,单引号也可以帮助您避免多余的反斜杠:

Also, with most shells, single quotes will help you avoid extra backslashes:

ack '(?P<foo>\d{4}-\d{2}-\d{2})' foo.txt --output '$+{foo}'

这篇关于如何在ack输出中使用命名的正则表达式组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆