[英]grep with regular expression in command line
I'm interested in finding pattern like %CHILD_NAME%, %PARENT_NAME%, %ADDRESS% using regex and preferably recursively in the current directory Following is the grep command I am using 我对使用正则表达式(最好递归地)在当前目录中找到%CHILD_NAME%,%PARENT_NAME%,%ADDRESS%这样的模式感兴趣,以下是我正在使用的grep命令
grep -r "(.[A-Z]+[_]*[A-Z]+%)" *
When I use the same regex above at http://www.regexr.com , it does match %CHILD_NAME% but my command is not able to find this pattern in any file in current or sub directory. 当我在http://www.regexr.com上使用相同的正则表达式时,它确实与%CHILD_NAME%匹配,但我的命令无法在当前目录或子目录中的任何文件中找到此模式。
By default, grep uses basic regular expression and meta-characters like +
lose their meaning and need to be escaped. 默认情况下,grep使用基本的正则表达式,而+
等元字符会失去其含义,需要转义。 Remove the capturing group ( )
, escape the +
quantifiers and use an actual %
in place of .
删除捕获组( )
,转义+
量词,并使用实际的%
代替.
grep -r '%[A-Z]\+[_]*[A-Z]\+%' *
Although, you could probably use the following: 虽然,您可能会使用以下内容:
grep -r "%[A-Z_]\+%" *
First of all, you regex is too generic: at matches CHILD_NAME%
(without %
in the front) as well. 首先,您的正则表达式太笼统了:匹配CHILD_NAME%
(前面也没有%
)。 A better regex is: 更好的正则表达式是:
"%[A-Z]+(_[A-Z]+)*%"
Next, it is advisable to use the perl
interpretation of regexes using the -P
flag: 接下来,建议使用带有-P
标志的正则表达式的perl
解释:
grep -r -P "%[A-Z]+(_[A-Z]+)*%" .
You can also use the -E
flag here (extensive mode). 您也可以在此处使用-E
标志(扩展模式)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.