正则表达式(grep)需要多行搜索 [英] Regex (grep) for multi-line search needed
问题描述
可能重复:
如何在文件中搜索多行模式?使用pcregrep
我正在运行 grep
找到任何具有选择
,后跟词 customerName
的* .sql文件由从
。此select语句可以跨多行,并且可以包含制表符和换行符。
I'm running a grep
to find any *.sql file that has the word select
followed by the word customerName
followed by the word from
. This select statement can span many lines and can contain tabs and newlines.
我尝试过以下几种变体:
I've tried a few variations on the following:
$ grep -liIr --include="*.sql" --exclude-dir="\.svn*" --regexp="select[a-zA-Z0-
9+\n\r]*customerName[a-zA-Z0-9+\n\r]*from"
$ customerName [a-zA-Z0-9 + \\\
\r] p>但是,这只是永远运行。任何人都可以帮助我正确的语法吗?
This, however, just runs forever. Can anyone help me with the correct syntax please?
推荐答案
无需安装grep变体pcregrep,您可以使用grep进行多行搜索。
Without the need to install the grep variant pcregrep, you can do multiline search with grep.
$ grep -Pzo "(?s)^(\s*)\N*main.*?{.*?^\1}" *.c
说明:
-P
为grep激活perl-regexp(正常扩展的强大扩展)
-P
activate perl-regexp for grep (a powerful extension of regular extensions)
-z
在行尾禁用换行符,将其替换为空字符。也就是说,grep知道行尾是什么,但将输入视为一个大行。
-z
suppress newline at the end of line, subtituting it for null character. That is, grep knows where end of line is, but sees the input as one big line.
-o
打印只匹配。因为我们使用 -z
,整个文件就像一个大行,所以如果有匹配,整个文件将被打印;
-o
print only matching. Because we're using -z
, the whole file is like a single big line, so if there is a match, the entire file would be printed; this way it won't do that.
(?)
s)激活 PCRE_DOTALL
,这意味着。
找到任何字符或换行符
(?s)
activate PCRE_DOTALL
, which means that .
finds any character or newline
\N
查找除换行符以外的任何内容,即使 PCRE_DOTALL
\N
find anything except newline, even with PCRE_DOTALL
activated
。*?
在非法模式下查找。
^
查找行首
\1
对第一组的反向引用( \s *
)这是一个尝试找到相同缩进的方法
\1
backreference to first group (\s*
) This is a try to find same indentation of method
你可以想象,这个搜索打印的主要方法在C( *。c
)源文件。
As you can imagine, this search prints the main method in a C (*.c
) source file.
这篇关于正则表达式(grep)需要多行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!