简体   繁体   English

正则表达式查找不包含特定单词的行

[英]Regular expression to find lines not containing a certain word

I would like to find all occurrences of the below pattern. 我想找到以下模式的所有情况。

import com.pack1.pack2.blah1.className1 ;
import com.pack1.pack2.blah2.className2 ;
import com.pack1.DifferentPackage.blah3.className3 ;

I want to find all import statements which will match the third line. 我想找到与第三行匹配的所有导入语句。

Something like below 像下面这样

import com.pack1.!(pack2).*

I tried a few examples from tutorials - but couldn't achieve the objective. 我尝试了一些教程中的示例-但无法达到目标。

You are looking for a Negative Lookahead here. 您正在这里寻找否定前瞻

import com\.pack1\.(?!pack2\b).*

Live Demo 现场演示

The word boundary \\b asserts that on one side there is a word character, and on the other side there is not. 单词边界\\b断言,一侧有一个单词字符,而另一侧则没有。 Also since you state you are looking for lines, you may want to add beginning of string ^ and end of string $ anchors. 另外,由于您声明要查找行,因此可能要添加字符串^开始和字符串$锚的结尾。

^import com\.pack1\.(?!pack2\b).*$

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

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