需要多行搜索的正则表达式(grep) [英] Regex (grep) for multi-line search needed

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

问题描述

我正在运行 grep 以查找任何包含单词 select 后跟单词 customerName 后跟的 *.sql 文件词 from.这个选择语句可以跨越多行,并且可以包含制表符和换行符.

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+

]*customerName[a-zA-Z0-9+

]*from"

然而,这只是永远运行.任何人都可以帮助我使用正确的语法吗?

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 expressions)

-z 在行尾取消换行,用空字符代替.也就是说,grep 知道行尾在哪里,但将输入视为一行.

-z suppress newline at the end of line, substituting 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

.*? 在非贪婪模式下找到.,即尽快停止.

.*? find . in non-greedy mode, that is, stops as soon as possible.

^ 找到行首

1 反向引用第一组 (s*).这是试图找到方法的相同缩进.

1 backreference to the first group (s*). This is a try to find the same indentation of method.

如您所想,此搜索会打印 C (*.c) 源文件中的 main 方法.

As you can imagine, this search prints the main method in a C (*.c) source file.

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

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