Mark Aroni
Mark Aroni

Reputation: 623

Search eclipse workspace where string not starting with

In the eclipse workspace search (CTRL+H) what regular expression could be used to find occurrences of a word that do not begin with 2 leading forward slashes (/) - or in other words, are not commented?

For example //var_dump and // var_dump is what is not to be matched, but var_dump is what is to be matched.

Upvotes: 0

Views: 813

Answers (2)

diwhyyyyy
diwhyyyyy

Reputation: 6372

The following regex in the file search tab would do it:

(?<!//)[ \t]*var_dump

If you only care about the first word on a line, precede it with a caret:

^(?<!//)[ \t]*var_dump

The (?<!//) part is a negative lookbehind - it only matches thing that do not have the contents of the brackets before the thing you are looking for.

However, if you are looking for variables that are not commented rather than not preceded by the specific combo "//", you might be better off using a syntax-aware search, which depends on the language used. For example, this regex will still match /* var_dump */, which you may or may not want to happen. A syntax-aware search would know this is commented.

If you look in the Search window, you will see a "Java Search" tab and tabs for other plugins you might have (I have "C/C++ Search" and "Git Search" for example). In these tabs, you can choose to search only for functions, variable, classes, or whatever else makes sense in that language.

A further thing you might want is to hit Ctrl-Shift-G on a word and it will list all the references to this object in your code - very useful when trying to track down what is calling your function or using your variable.

Upvotes: 1

Chris Gerken
Chris Gerken

Reputation: 16392

If you're looking for a variable name (or the name of any other kind of Java element), you could use the Java search instead of the File search. You don't have regular expression support, but the search knows you're looking for a variable/class name and so you don't have to worry about surrounding syntax.

Upvotes: 0

Related Questions