1

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.

1 Answer 1

4

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. 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).*$
Sign up to request clarification or add additional context in comments.

4 Comments

+1, It cleared My doubts about Negative - lookarounds
how about import com.pack1.pack20.blah1.className3 ;
Thanks . I will try this - That would really save a lot of time for me.
I tried this using the regex search in TextPad editor - it did not work . I will check what version of regex is being used . I searched for the string "import com\.pack1\.(?!pack2\b).*" - Should I do something else ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.