0

Suppose you had a Java file which contained lots of single letter variable names like a,x,f etc, and you wanted to change them to something more meaningful. You could try replacing every occurrence of ‘f’ with ‘filename’, but that would mess up all of the places where ‘f’ occurs inside a word (for example in the keyword if ). How would you go about correcting this problem using regular expression search and replace?

I need to write this in Emacs, using word boundaries. Since I am new, does using the Matcher Class apply?

3 Answers 3

0

If you must do this using native emacs: Word boundaries are a reasonable search boundary. The Matcher class is a Java construct. If you were writing code to do the search and replace. Since you are doing this completely in emacs, it wouldn't apply.

There is a caveat that these variables can't be used from multiple source files or you will mess things up.

I wouldn't do this with raw emacs though. I'd use an IDE refactoring tool. Emacs even has a Java Refactoring that you can install. Using a refactoring tool, the tool understands the syntax and will be more specific than a regular expression.

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

3 Comments

Is there anyway I could have the answer from you?
The answer to what? I provided an answer to what I understood to be the question. If you are looking for something else, please clarify.
I got the answer myself.
0

Here's an answer based on this question:

  1. Before doing this kind of stuff with any tool, version-control everything. I recommend git.
  2. M-x find-name-dired.
  3. Enter the path to your project root, e.g. ~/Dropbox/source/java/.
  4. Enter the java file wildcard: *.java. You'll end up in a dired buffer with all the java files in your project root.
  5. t - this will mark all the files in dired. It means they'll be the target of the next operation.
  6. Q - this calls dired-do-query-replace-regexp. Now you're prompted for what you want replaced - enter e.g. \bf\b. Next, enter the replacement e.g. filename.
  7. Now Emacs will walk you though all occurrences of \bm\b in all your java files. You have to type each time either SPC to accept or n to skip the current instance. You can see all the options while replacing by typing ?.
  8. Finally, C-x s.

Comments

0

\bj\b where the letter i is supposed to be found. So anything bound between the b's is the character to be found.

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.