简体   繁体   English

使用Emacs和正则表达式搜索并替换Java源文件中的字符串

[英]Search and replace strings in Java source files using Emacs and regular expressions

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. 假设您有一个Java文件,其中包含许多单字母变量名称,例如a,x,f等,并且您想将其更改为更有意义的名称。 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 ). 您可以尝试将所有出现的'f'替换为'filename',但这会弄乱单词中所有出现'f'的位置(例如,关键字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. 我需要使用单词边界在Emacs中编写此代码。 Since I am new, does using the Matcher Class apply? 由于我是新手,因此使用Matcher类是否适用?

If you must do this using native emacs: Word boundaries are a reasonable search boundary. 如果必须使用本机emacs执行此操作:单词边界是一个合理的搜索边界。 The Matcher class is a Java construct. Matcher类是Java构造。 If you were writing code to do the search and replace. 如果您正在编写代码来进行搜索和替换。 Since you are doing this completely in emacs, it wouldn't apply. 由于您是完全在emacs中完成此操作,因此它不适用。

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. 我不会用原始emacs做到这一点。 I'd use an IDE refactoring tool. 我将使用IDE重构工具。 Emacs even has a Java Refactoring that you can install. Emacs甚至可以安装Java重构 Using a refactoring tool, the tool understands the syntax and will be more specific than a regular expression. 使用重构工具,该工具可以理解语法,并且比正则表达式更加具体。

Here's an answer based on this question : 这是基于这个问题的答案:

  1. Before doing this kind of stuff with any tool, version-control everything . 在使用任何工具进行此类操作之前,请对所有内容进行版本控制。 I recommend git . 我推荐git
  2. Mx find-name-dired . Mx find-name-dired
  3. Enter the path to your project root, eg ~/Dropbox/source/java/ . 输入项目根目录的路径,例如~/Dropbox/source/java/
  4. Enter the java file wildcard: *.java . 输入Java文件通配符: *.java You'll end up in a dired buffer with all the java files in your project root. 最后,您将在项目根目录下的所有Java文件都放在一个dired缓冲区中。
  5. t - this will mark all the files in dired. t-这将标记所有文件为重复。 It means they'll be the target of the next operation. 这意味着它们将成为下一个操作的目标。
  6. Q - this calls dired-do-query-replace-regexp . Q-这称为dired-do-query-replace-regexp Now you're prompted for what you want replaced - enter eg \\bf\\b . 现在,系统会提示您输入要替换的内容-输入\\bf\\b Next, enter the replacement eg filename . 接下来,输入替换内容,例如filename
  7. Now Emacs will walk you though all occurrences of \\bm\\b in all your java files. 现在,Emacs将带您浏览所有Java文件中所有\\bm\\b出现。 You have to type each time either SPC to accept or n to skip the current instance. 您每次必须输入SPC来接受,或者每次输入n来跳过当前实例。 You can see all the options while replacing by typing ? 键入时,您可以在替换时看到所有选项 .
  8. Finally, Cx s . 最后, Cx s

\\bj\\b where the letter i is supposed to be found. \\ bj \\ b应该在找到字母i的位置。 So anything bound between the b's is the character to be found. 因此,b之间的任何绑定都是要找到的字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM