开发者

Regex expression to match string not containing other two substrings

So,

I need a regex expression to match a string that does not include a couple of substring开发者_StackOverflow中文版s. I want regex to match all lines containing /paths in them unless they contain string1 or string2 prior to /paths

Do not match:

foo = string1("/paths

string2 + "/paths

string1 = "/paths

"#{string2}/paths

But matches:

source("/paths

bg : "/paths

Thanks


Have a look at this in particular the section on positive and negative look-behinds.

something similar to (this is untested) this:

(?<!*?string1*?|*?string2*?)"/paths"


If you use a flavor that supports lookaheads, this will work:

^(?:(?!string1|string2|/paths).)*/paths


You may want to go around another way to do this with and if/else, ie check for string1 or string2 in your string, if its found, then don't process.

if "string1" in string or "string2" in string then
  ...
else
  process
end if 


I used the following sed command to do so
sed -i -e '/string1\|string2/!s/paths/fix_paths/gI' FILE_NAME

It does not do exactly search for the strings prior my paths, but it matched what I intended to match and worked for my specific case.

Thanks all for your help


You one grep for what you want and another with -v for what you don't what:

$ grep want_string input_file | grep -v unwant_string
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜