Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace strings in batches with regular expressions by IDEA

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how IDEA uses regular expressions to replace strings in batches, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

Due to the initial extensive expansion, there are a lot of magic-like id left in the company's project code, which makes it difficult to maintain. So I was given the task of replacing these id with static variables.

There are more than ten kinds of id, distributed in ten files, a total of more than 60 places, it is almost impossible to find and replace manually.

In Idea, the menu bar Edit- > find- > replace in path can turn on the full file search function to find and replace the contents of all files in the project.

Now we have code like this, where abcd is written dead id:

String id= "abcd" .equals (str)

To be replaced by:

String id=ConstantService.getBy ("abcd") .equals (str)

If I only used a normal match, I would want to look for "String id=" and replace it with "String id=ConstantService.getBy (").

However, there is a lot of code in the format of "String id=" in the project, and ordinary matches are likely to hurt other code.

So I use regular matching. Click the ". *" icon on the right to open the regular matching pattern.

The code that needs to be replaced matches this matching pattern:

String id=\ "(\ w+)\" .equals\ (str\)

Can be replaced by:

String id=ConstantService.getBy ("$1") .equals (str)

In the matching pattern, the written id can be represented by "(\ w +)". The outermost is the escaped quotation mark. Parentheses represent a matching group where\ w matches the character and the + sign indicates that the character appears 1 or more times. This pattern is consistent with the id written in the project.

$1 in the replacement string represents the first matching group in the matching string. Therefore, instead of specifying each id over and over again, the replacement string automatically replaces the matching id into the result.

In this way, this unimaginably complex task can be easily solved. So much for this article about Idea replacing strings in batches with regular expressions.

Thank you for reading this article carefully. I hope the article "how to use IDEA to replace strings in batches with regular expressions" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report