简体   繁体   English

在Java中使用正则表达式匹配字符串并生成序列

[英]Match String and generate sequence using regex in Java

I'm really curious if I can match an example and every time I found the repetition I increment the number. 我真的很好奇我能否匹配一个示例,每次发现重复时,我都会增加数字。 I try to visualize it on example. 我尝试在示例中将其可视化。 Let's say I have some data like this: 假设我有一些这样的数据:

HashMap<String, Integer> map = new HashMap<>();
HashMap<String, Integer> map = new HashMap<>();
HashMap<String, Integer> map = new HashMap<>();
...
...
...
HashMap<String, Integer> map = new HashMap<>();

what I want to achieve is: 我想要实现的是:

HashMap<String, Integer> map1 = new HashMap<>();
HashMap<String, Integer> map2 = new HashMap<>();
HashMap<String, Integer> map3 = new HashMap<>();
...
...
...
HashMap<String, Integer> map50 = new HashMap<>();

I'm working in InteliJ so I will use ctrl + shift + r shortcut to replace string occurrences. 我在InteliJ中工作,因此我将使用ctrl + shift + r快捷方式替换出现的字符串。 Is there any kind of reg ex which can let me create the sequence? 是否有可以让我创建序列的reg ex

While it is indeed possible to match incrementing integers with a regular expression (as you can see here ) you cannot store the number of occurred matches because a regular expression always needs to be nested in some kind of programming language to be used that way. 虽然确实可以用正则表达式匹配递增的整数(如您在此处看到的那样),但是您不能存储已发生的匹配数,因为正则表达式始终需要嵌套在某种编程语言中才能使用。 Writing a Java application that stores the number of matches in a variable (like in this answer) 编写将匹配数量存储在变量中的Java应用程序(例如答案)

Unfortunately, as far as I know, the replacement function of IntelliJ doesn't have a functionality to store the number of matches and add it to the replacement. 不幸的是,据我所知,IntelliJ的替换功能没有存储匹配数并将其添加到替换中的功能。

Like Gorazd already said, you can use the IntelliJ plugin called String Manipulation which will solve your problem. 就像Gorazd已经说过的那样,您可以使用名为String Manipulation的IntelliJ插件来解决您的问题。 You can find an answer to a similar (if not the same) question with some visualization of the plugin here . 您可以在此处通过插件的可视化找到类似(如果不相同)问题的答案。

You can do it with IntelliJ but only with a plugin. 您可以使用IntelliJ进行操作,但只能使用插件。 It's called String Manipulation . 这称为String Manipulation

Once installed, you need to have all variabled called map1 . 安装后,您需要将所有变量称为map1 Then simply select all the lines, right click on the first and select Increment Duplicates from the String Manipulation menu. 然后只需选择所有行,右键单击第一行,然后从“ 字符串操作”菜单中选择“ 增量重复 ”。

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

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