0

I need to move the 4th dimension of my Java array to the 1st dimension:

myArray[a][...][...][b]

to

myArray[b][...][...][a]

I need to do this for all dimension names. My idea is to do this using the find and replace feature in Atom using regular expressions, but I am not familiar enough with them to do it quickly. Basically, this is what I am looking for:

myArray([][][])([])

to

myArray([])([][][])

It would be wonderful if you could provide me with the required Find and Replace fields in order to do this.

Bill
  • 89
  • 1
  • 7
  • Just find [b] and replace with [a] then do the opposite but skip the 4th dimension at the end?? – Bayleef Jan 21 '19 at 18:13
  • My problem is that the array takes many forms and the project is massive. For example, at times the array is in this form: myArray[a][b][c][d] and at times it is in this form: myArray[e][f][g][h]. I need to account for anything that could possibly be in those brackets. – Bill Jan 21 '19 at 18:17

2 Answers2

2

Use this regex:

myArray(\[.+?])(\[.+?]\[.+?])(\[.+?])

And this replacement:

myArray$3$2$1

Explanation:

The regex is matching myArray followed by three sets of square brackets. It captures the first pair of brackets into group 1, second and third pairs into group 2, and the last pair into group 3.

The replacement is myArray, followed by whatever is in group 3, then whatever is in group 2, and finally whatever is in group 1.

Demo

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • If one of the index expressions contains itself `[ ... ]` this would not work. – Henry Jan 21 '19 at 18:21
  • This is precisely the answer that I was looking for. I will now be able to build upon this for future applications. Thanks, and I will accept this answer as soon as possible. – Bill Jan 21 '19 at 18:23
  • What does the question mark do here? I removed all of the question marsk and got fewer hits. – Bill Jan 21 '19 at 18:34
  • @Bill The question mark means "lazy". See https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions – Sweeper Jan 21 '19 at 18:34
0

This also works:

Find: myArray(\[[^\]]*\])(\[[^\]]*\]\[[^\]]*\])(\[[^\]]*\])

Replace: myArray$3$2$1

Bill
  • 89
  • 1
  • 7