0

I want to find all occurrences of a variable name in a file, let's say variable test:

 int test;

but i don't want to match the variable name when it's inside a string, like

String s = "This is a test!";

I tried ([^\"])([a-zA-Z_$][\\w$]*)([^\"]), but it won't work.

5
  • 1
    If you want to take care of all type of variable declarations in Java then RegEx is NOT the right way to do it. It is next to impossible to capture all possible kind of variable declarations. Commented Mar 8, 2012 at 14:23
  • What programming language is the file in? Anyway, this is not easily done with regex, as you need to parse all language constructs to get it right, besides quotes, there are different types of comments and depending on language other constructs. Commented Mar 8, 2012 at 14:23
  • The file contains Java programming language. Well, then what is the best way to find all occurrences of a variable in a file, other than RegEx? Commented Mar 8, 2012 at 14:27
  • @user1019710 read my answer, i guessed what you were trying to do. Commented Mar 8, 2012 at 14:28
  • 1
    Many IDE's have such refactor functionality: perhaps that is an option? Commented Mar 8, 2012 at 14:53

3 Answers 3

2

I’m afraid Regular Expressions are not the best fit for your problem. Since there are a lot of semantics to consider when parsing source code, it is very unlikely that you can come up with a reliable expression, that doesn’t get confused by things like escaped quotes within strings.

A better way to parse source code (and reliably detect things like variable names) is to use a generated parser, that knows about the grammar of the file to parse. SableCC is designed for this and it also conveniently provides a grammar file for Java 1.5.

It will basically tokenize the given source code and add type information to each token. This way you can simply iterate over all tokens and rebuild the source while replacing every token that matches your search term and is of type variable.

Sign up to request clarification or add additional context in comments.

4 Comments

As far as i know, a parser verifies the syntax. Let's say i want to rename a variable and all its occurrences. Can i do this?
@user1019710: I guess it's surely possible.
I may have answered that question accidentally with the paragraph I just added to my answer, before I even saw your comment. The parser will simply give you a structured look at the input file, so you know exactly what each character sequence represents. Anything beyond that is up to you.
Well, i need to implement some kind of obfuscator for Java. I think the right way is implementing a parser first.
1

As I said in the comment, generally using regex for this is not a good idea. You should use some kind of parer for this.

But anyway here is a simple hack that will work for some cases:

(?xm) \b test \b
(?=
    (?:[^\n"\\]+|\\.)*
    (?:(?:"(?:[^\n"\\]+|\\.)*){2})*
    $
)

Java quoted:

"(?m)\\btest\\b(?=(?:[^\n"\\\\]+|\\\\.)*(?:(?:"(?:[^\n"\\\\]+|\\\\.)*){2})*$)"

Some comments and other things will break it.

1 Comment

Isn't there a simpler way to do this? It takes a very very long time to find all occurrences of a variable in a file of small size...
0

Maybe it is an idea to temporarily cut all string out of the source code and then search for the variable name.

Assuming the source code is valid (no syntax errors), you can cut everything from the first occuring double quote (") to the next double quote.

Notice that variable names with just one character (like d) will require some additional code, for d is also used for forcing the compiler as interpreting the preceding number as a double (e.g. double dbl = 6d).

EDIT: I was assuming that you wanted to build an application or piece of code which lightweight-checked for variable names.
If you work inside an editor, I recommend you to use an advanced editor like Netbeans or Eclipse.
Otherwise, if you want to also check for correct syntax, you'll need to build your own interpreter (or download some from internet).

Comments

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.