Reputation: 30042
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
Reputation: 786261
File Search
tabRegular expression
radio button".*?Eggs[^"]*"
in Containing Text
field on topUpvotes: 4
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
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