Skip to content
Advertisement

Java config file multidimensional array

I have a problem. For my code I have a config file with the following content:

JavaScript

To read this config file, I have the following code:

JavaScript

But now I need to change the format of the names to:

JavaScript

The output varialbe has to be of the type: String[][] with as result:

JavaScript

What is the best way to achieve this?

Advertisement

Answer

Depends on what the input could look like. The safest way would probably be to use a proper parser (and maybe a different file format).

If the list is always in the form [[Name], [Name]] and Name never contains brackets, a simple way could be to use a more specialized regex, e.g. (?<=]),s*(?=[).

Rundown on the regex:

  • (?<=]): a positive look-behind, i.e. any match must follow a ].
  • ,s*: the actual match to split at (and remove), i.e. a comma followed by any whitespace
  • (?=[)"): a positive look-ahead, i.e. any match must be followed by a [.

Finally, split each of the names by , to get the 2D array:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement