简体   繁体   English

Sed正则表达式不匹配“两个或两个”内部组

[英]Sed regex not matching 'either or' inner group

I would like to match multiple file extensions passed through a pipe using sed and regex. 我想匹配使用sed和regex通过管道传递的多个文件扩展名。

The following works: 以下作品:

sed '/.\(rb\)\$/!d'

But if I want to allow multiple file extensions, the following does not work. 但是,如果我想允许多个文件扩展名,则以下操作不起作用。

sed '/.\(rb\|js\)\$/!d'
sed '/.\(rb|js\)\$/!d'
sed '/.(rb|js)\$/!d'

Any ideas on how to do either/or inner groups? 关于如何做一个或一个或多个内部小组的想法?

Here is the whole block of code: 这是整个代码块:

#!/bin/sh
files=`git diff-index --check --cached $against | # Find all changed files
       sed '/.\(rb\|js\)\$/!d'                  | # Only process .rb and .js files
       uniq`                                      # Remove duplicate files

I am using a Mac OSX 10.8.3 and the previous answer does not work for me, but this does: 我使用的是Mac OSX 10.8.3,上一个答案对我不起作用,但是可以:

sed -E '/\.(rb|js)$/!d'  

Note: use -E to 注意:使用-E

Interpret regular expressions as extended (modern) regular expressions rather than basic regular expressions (BRE's). 将正则表达式解释为扩展(现代)正则表达式,而不是基本正则表达式(BRE)。

and this enables the OR function | 这将启用OR功能| ; ; other versions seem to want the -r flag to enable extended regular expressions. 其他版本似乎希望-r标志启用扩展的正则表达式。 Note that the initial . 注意初始. must be escaped and the trailing $ must not be. 必须转义,结尾的$一定不能。

Try something like this: 尝试这样的事情:

sed '/\.\(rb\|js\)$/!d'

or if you have then use -r option to use extended regular expression for avoiding escaping special character. 或者,如果您具有-r选项,则可以使用扩展的正则表达式来避免转义特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM