在Java代码中搜索某个字符串并替换它
在其他人编写的这段java代码中,人们使用了null
检查条件作为null!=abc
或null==abc
,但是我需要将其分别更改为 abc!=null
和 abc==null
。
有人告诉我,这可以使用正则表达式非常轻松地完成,因为到目前为止我正在执行手动任务,但搜索它然后手动替换它。
In this java code that some one else has written, people have used null
check condition as null!=abc
or null==abc
, but I need to change it to abc!=null
and abc==null
respectively.
Someone told me that this can be done using regular expressions very easily, as till now I was performing a manual task, but searching it and then replacing it manually.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道布尔表达式右侧的所有要求,但假设它是变量名,
\w
将是大致准确的搜索(请参阅http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html 了解变量名称的完整规范)。搜索字符串:
替换字符串
I do not know all the requirements for what could be on the right side of the boolean expression, but assuming it is a variable name,
\w
will be a roughly accurate search (see http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html for the full specification of variable names).Search string:
Replace string
使用正则表达式无法正确完成此操作。
考虑
如果您只想更改代码,即上面的第三个
null==abc
,那么您可以通过天真地使用正则表达式替换来更改代码,从而对字符串和注释内容进行意外更改。并且您要小心不要更改
为
或更改
为
如果您对潜在的字符串和注释更改感到满意,并且要检查更改以确保没有中断任何方法调用,那么您始终可以这样做这是 Perl 中的。
It cannot be done correctly with regular expressions.
Consider
If you want to change only the code, the third
null==abc
above, then you can get unintended changes to string and comment content by naively using regular expression replaces to change code.And you want to be careful to not change
to
or to change
to
If you're OK with potential string and comment changes, and are going to review the changes to make sure you didn't break any method calls, then you can always do this in perl.