在Java文件中搜索和替换

时间:2020-12-19 16:48:44

I need to change the logging design in my application. Currently my application uses a utility class to all the all the errors, info and debug statement. I need to change that. For example I need to change all the occurrences of

我需要在我的应用程序中更改日志记录设计。目前我的应用程序使用实用程序类来处理所有错误,信息和调试语句。我需要改变它。例如,我需要更改所有出现的事件

Log.send("XXX", Log.DEBUG); 

to

logger.debug("XXX"); 

and

Log.send("XXX", Log.INFO); 

to

logger.info("XXX");  or

Can I do it using any tool or using regular expression in editors like Notepad++ or textpad?

我可以使用任何工具或在Notepad ++或textpad等编辑器中使用正则表达式吗?

3 个解决方案

#1


1  

Using a regex search and find (I'm using atom's style):

使用正则表达式搜索并查找(我正在使用atom的样式):

FIND: Log.send\((\"\w+\"), Log.DEBUG\);

查找:Log.send \((\“\ w + \”),Log.DEBUG \);

REPLACE: logger.debug($1)

FIND: Log.send\((\"\w+\"), Log.INFO\);

查找:Log.send \((\“\ w + \”),Log.INFO \);

REPLACE: logger.info($1)

#2


0  

In Notepad++ you can use regex like so

在Notepad ++中,您可以像这样使用正则表达式

logger.debug

Find:

Log\.send\("([^"]+)", Log\.DEBUG\);

Replace:

logger\.debug\("\1"\);

logger.info

Find:

Log\.send\("([^"]+)", Log\.INFO\);

Replace:

logger\.info\("\1"\);

I tested this in eclipse and it works. For Netbeans, just replace \1 with $1. If you want to be lazy and just use one regex find-and-replace match for the two, then you can use this:

我在eclipse中对此进行了测试,但它确实有效。对于Netbeans,只需用$ 1替换\ 1。如果你想要懒惰并且只为这两个使用一个正则表达式查找和替换匹配,那么你可以使用这个:

Find:

Log\.send\("([^"]+)", Log\.([^\)]+)\);

Replace

logger\.\L\2\E\("\1"\);

The difference in this is that I capture DEBUG and INFO too, then use them as the method name for the logger. However, the keywords are in upper case, so I put them to lowercase first (hence, the \L and \E to cut off the lower case.

不同之处在于我也捕获了DEBUG和INFO,然后将它们用作记录器的方法名称。但是,关键字是大写的,所以我先把它们放到小写字母(因此,\ L和\ E来切断小写字母。

#3


0  

If you have access to vi on cygwin(windows) or shell (os x or linux), or even other linux commands should make this very simple. Its another way of doing this. Also you don't have to open each file, if you do it right it should be one command on shell for the entire project source code.

如果你可以访问cygwin(windows)或shell(os x或linux)上的vi,甚至其他linux命令都应该让这个变得非常简单。这是另一种方式。此外,您不必打开每个文件,如果您这样做,它应该是shell上的一个命令,用于整个项目源代码。

Example: The below code looks for test search string for all files in current directory (recursively) and replaces it with 'replacetext'

示例:下面的代码查找当前目录中所有文件的测试搜索字符串(递归)并将其替换为'replacetext'

grep -r -l 'test' . | sort | uniq | xargs perl -e "s/test/replacetext/" -pi

#1


1  

Using a regex search and find (I'm using atom's style):

使用正则表达式搜索并查找(我正在使用atom的样式):

FIND: Log.send\((\"\w+\"), Log.DEBUG\);

查找:Log.send \((\“\ w + \”),Log.DEBUG \);

REPLACE: logger.debug($1)

FIND: Log.send\((\"\w+\"), Log.INFO\);

查找:Log.send \((\“\ w + \”),Log.INFO \);

REPLACE: logger.info($1)

#2


0  

In Notepad++ you can use regex like so

在Notepad ++中,您可以像这样使用正则表达式

logger.debug

Find:

Log\.send\("([^"]+)", Log\.DEBUG\);

Replace:

logger\.debug\("\1"\);

logger.info

Find:

Log\.send\("([^"]+)", Log\.INFO\);

Replace:

logger\.info\("\1"\);

I tested this in eclipse and it works. For Netbeans, just replace \1 with $1. If you want to be lazy and just use one regex find-and-replace match for the two, then you can use this:

我在eclipse中对此进行了测试,但它确实有效。对于Netbeans,只需用$ 1替换\ 1。如果你想要懒惰并且只为这两个使用一个正则表达式查找和替换匹配,那么你可以使用这个:

Find:

Log\.send\("([^"]+)", Log\.([^\)]+)\);

Replace

logger\.\L\2\E\("\1"\);

The difference in this is that I capture DEBUG and INFO too, then use them as the method name for the logger. However, the keywords are in upper case, so I put them to lowercase first (hence, the \L and \E to cut off the lower case.

不同之处在于我也捕获了DEBUG和INFO,然后将它们用作记录器的方法名称。但是,关键字是大写的,所以我先把它们放到小写字母(因此,\ L和\ E来切断小写字母。

#3


0  

If you have access to vi on cygwin(windows) or shell (os x or linux), or even other linux commands should make this very simple. Its another way of doing this. Also you don't have to open each file, if you do it right it should be one command on shell for the entire project source code.

如果你可以访问cygwin(windows)或shell(os x或linux)上的vi,甚至其他linux命令都应该让这个变得非常简单。这是另一种方式。此外,您不必打开每个文件,如果您这样做,它应该是shell上的一个命令,用于整个项目源代码。

Example: The below code looks for test search string for all files in current directory (recursively) and replaces it with 'replacetext'

示例:下面的代码查找当前目录中所有文件的测试搜索字符串(递归)并将其替换为'replacetext'

grep -r -l 'test' . | sort | uniq | xargs perl -e "s/test/replacetext/" -pi