带有多行扩展正则表达式的grep [英] grep with extended regex over multiple lines

查看:157
本文介绍了带有多行扩展正则表达式的grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在多行上获得一种模式.我想确保我要查找的行以\ r \ n结尾,并且在某些时候后面有特定的文本.我遇到的两个问题是,我经常在分组中出现括号不匹配的情况,或者在没有括号时得到正匹配的结果.这是两个简单的示例.

I'm trying to get a pattern over multiple lines. I would like to ensure the line I'm looking for ends in \r\n and that there is specific text that comes after it at some point. The two problems I've had are I often get unmatched parenthesis in groupings or I get a positive match when there is none. Here are two simple examples.

echo -e -n "ab\r\ncd" | grep -U -c -z -E $'(\r\n)+.*TEST'

grep:不匹配(或\(
那里到底有什么比拟的?我不明白.

grep: Unmatched ( or \(
What exactly is unmatched there? I don't get it.


echo -e -n "ab\r\ncd" | grep -U -c -z -E $'\r\n.*TEST'

1
字符串中没有TEST,那么为什么匹配项返回1呢?

1
There is no TEST in the string, so why does this return a count of 1 for matches?


我在Ubuntu 14上使用grep(GNU grep)2.16.谢谢

I'm using grep (GNU grep) 2.16 on Ubuntu 14. Thanks

推荐答案

您可以使用-P代替gc grep中的-P来支持PCRE,以使用高级正则表达式,如下所示:

Instead of -E you can use -P for PCRE support in gnu grep to use advanced regex like this:

echo -ne "ab\r\ncd" | ggrep -UczP '\r\n.*TEST'
0

echo -ne "ab\r\ncd" | ggrep -UczP '\r\n.*cd'
1

grep -E仅在单行输入中匹配.

grep -E matches only in single line input.

这篇关于带有多行扩展正则表达式的grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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