Garrett Hall
Garrett Hall

Reputation: 30042

How to search for a Java string in Eclipse?

I need to search for instances of a character sequence in a Java string using Eclipse. Is there an easy way or regex to do this?

Example: Search for Eggs should match

String str = "Eggs and toast."

and not match

Eggs e = new Eggs()

Upvotes: 5

Views: 2776

Answers (4)

anubhava
anubhava

Reputation: 786261

  • Under search menu (press ^H)
  • Go to File Search tab
  • check Regular expression radio button
  • Enter text ".*?Eggs[^"]*" in Containing Text field on top
  • Click on Search button

Upvotes: 4

Bohemian
Bohemian

Reputation: 425398

Searching for delimited text using regex is dodgy, however this will basically hit your target (and not too many others):

".*Eggs.*"

Use Search > File > with regex with exactly this search term (ie including the quotes)

This will however also hit lines like this:

 x = "foo" + new Eggs() + "bar";

Also note that you don't need to delimit the double quote, because it's just a literal as far as regex is concerned

Upvotes: 0

dty
dty

Reputation: 18998

In general this is very hard because what you want to search for is not "regular" in the sense of "regular expression".

One of the answers you've had suggests:

\".*Eggs.*\"

which is pretty good, but will still match, for example,

System.out.println("There are " + new Eggs().count() + " eggs");

In general, there is not going to be a regular expression which does exactly what you want.

Upvotes: 3

Mike Bockus
Mike Bockus

Reputation: 2079

Would something like this work for you?

\".*Eggs.*\"

Upvotes: 3

Related Questions