0

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. Is there any kind of reg ex which can let me create the sequence?

shurrok
  • 795
  • 2
  • 13
  • 43
  • I think it is a good practice to give an opinion before before giving a down vote :) – shurrok Nov 22 '17 at 11:11
  • 1
    Regex doesn't support persistent variables or increment, so no. If all the occurrences are in 1 file, it would be fairly easy to just write some Java code to do this. – Bernhard Barker Nov 22 '17 at 11:23

2 Answers2

1

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)

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.

Like Gorazd already said, you can use the IntelliJ plugin called String Manipulation which will solve your problem. You can find an answer to a similar (if not the same) question with some visualization of the plugin here.

Tornadowarnung
  • 368
  • 3
  • 11
0

You can do it with IntelliJ but only with a plugin. It's called String Manipulation.

Once installed, you need to have all variabled called map1. Then simply select all the lines, right click on the first and select Increment Duplicates from the String Manipulation menu.

Gorazd Rebolj
  • 803
  • 6
  • 10
  • I tried it, but when I `put()` data after creating `HashMap` object this plugin can't deal with that :( – shurrok Nov 22 '17 at 14:56