如何使用正则表达式反转匹配/黑名单? [英] How to inverse match/blacklist with regex?

查看:81
本文介绍了如何使用正则表达式反转匹配/黑名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过这个问题:Regular expression to match一行不包含单词?

但我无法让它工作.我有一个 shell 脚本,我正在使用

But I can't get it to work. I have a shell script and I'm using

string1.*string2.*string3

按顺序在文件中搜索 3 个单词.但我想更改它,以便如果 badword5 位于该文件中这些单词之间的任何位置,则不存在与 grep 的正则表达式匹配.

To search for 3 words in a file, in that order. But I want to change it so that if badword5 is anywhere in between those words in that file, there is no regex match with grep.

所以这应该匹配:

./testing/test.txt:   let prep = "select string1, dog from cat",
          " where apple = 1",
          " and string2 = 2",
          " and grass = 8",
          " and string3 = ?"

但这不应该:

   ./testing/test.txt:   let prep = "select string1, dog from cat",
          " where apple = 1",
          " and string2 = 2",
          " and grass = 8",
          " and badword5 = 4", 
          " and string3 = ?"

我尝试失败:

string1((?!badword5)|.)*string2((?!badword5)|.)*string3

整个脚本:

find . -name "$file_to_check" 2>/null | while read $FILE
do
   tr '\n' ' ' <"$FILE" | if grep -q "string1.*string2.*string3"; then echo "$FILE" ; fi
done >> $grep_out

推荐答案

"在一个文件中搜索 3 个单词,按这个顺序.但我想改变它,以便如果 badword5 在任何地方在该文件中的这些词之间,没有与 grep 的正则表达式匹配."

确实,搜索模式延伸了多行.
让我们暂时放弃 grep 并尝试不同的东西:

Indeed, and the search pattern stretches multiple lines.
let's drop grep for the moment and try something different:

#!/bin/bash

find . -name "$file_to_check" 2>/dev/null | while read FILE
do
    SCORE=0
    tr ' ' '\n' <"$FILE" | while read WORD
    do
        case $WORD in
        "word1"    ) [ $SCORE = 0 ] && SCORE=1               ;;
        "word2"    ) [ $SCORE = 1 ] && SCORE=2               ;;
        "word3"    ) [ $SCORE = 2 ] && echo "$FILE" && break ;;
        "badword5" ) SCORE=0                                 ;;
        esac
    done        
done >grep_out

case 行做以下事情:

the case lines do the following thing:

"    word1"      )    [ $SCORE     =       0 ] &&      SCORE  =       1  ;;
when word1 is found: and SCORE is equal to 0 then make SCORE equal to 1
when word2 is found: and SCORE is equal to 1 then make SCORE equal to 2
when word3 is found: and SCORE is equal to 2 then print filename and break out of the inner loop.

这篇关于如何使用正则表达式反转匹配/黑名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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