Java Split excluding rank

2

I receive a String in java with a value format and range [x,y,z] so that the input String is valor[x,y,z] (example 20[4Y,2W,4H] ).

I can have values with a condition or (represented with a ',') so that the String of input can be 20[4Y,2W,4H] , 10[2Y,1W,5H] .

I'm trying to get these two string 20[4Y,2W,4H] and 10[2Y,1W,5H] but when doing the split I also take into account those within [ ] .

Is there a way to make split excluding those within [ ] without having to chain splits ?

The range is optional so you would also have to chain if to check whether it was entered or not.

    
asked by Roger Llaveria 13.02.2018 в 12:47
source

2 answers

2

I could achieve it using the overhead that accepts a regular expression:

  String data = "20[4Y,2W,4H] , 10[2Y,1W,5H]";

   String[] parametros = data.split("(?<=])\s*[,]\s*");

   for(String a : parametros)
   {
       System.out.println(a);// imprime 20[4Y,2W,4H] y 10[2Y,1W,5H]
   }

The idea was to find the characters that separate one value from the other, in this case, the , . But since there are several commas, what I did was to look for a closing bracket ] followed by a comma , .

Explanation of the expression:

(?<=]) = Find the text that ends with ] but do not include it in the result.

\s* = Followed by 0 or more spaces

[,] = Followed by a comma

\s* = Followed by 0 or more spaces

    
answered by 13.02.2018 в 13:35
0
  

Input String is valor[x,y,z] (value and range)
  The range is optional .

     

make split excluding those within [ ]
  Example: 20[4Y,2W,4H] , 10[2Y,1W,5H] - > 20[4Y,2W,4H] and 10[2Y,1W,5H]


If the space only appears around the comma that separates values, you could use " , " as a delimiter.

But if the range is optional, there could be values such as 20,10[2Y,1W,5H] where you could not use the characters around the comma.


It can be addressed in 2 ways, both using a regex:

  • Separate in commas that are not followed by a ] (when there is not a [ in the middle.

    String[] elementos = data.split(" *, *+(?![^]\[]*])");
    
    • *, *+ any number of spaces, a comma, and any number of spaces.
    • (?![^]\[]*]) construction (?! ... ) is a negative inspection , or negative lookahead . Find that the current position is not followed by:
      • [^]\[]* any characters that are not ] ni [
      • ] and the closure of brackets that would mean that it is within a range.

  • Match with each of the parties (instead of doing a split)

    For that we use a regular expression that matches the value and, optionally with each of the 3 values of the range:

    (\d+)(?:\[(\d+)Y,(\d+)W,(\d+)H])?
    

    In this way, we already have all the values separated.

    Code:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
     
    final String regex = "(\d+)(?:\[(\d+)Y,(\d+)W,(\d+)H])?";
    final String texto = "1,2,3,20[4Y,2W,4H] , 10[2Y,1W,5H],4 , 5[10Y,20W,5H]";
    
    // así se compila siempre un regex
    final Pattern pattern = Pattern.compile(regex);
    final Matcher matcher = pattern.matcher(texto);
    
    //y así se buscan siempre todas las coincidencias
    while (matcher.find()) {
        //asignamos el valor capturado por cada grupo
        String valor  = matcher.group(1);
        String rangoY = matcher.group(2);
        String rangoW = matcher.group(3);
        String rangoH = matcher.group(4);
    
        //imprimimos en consola
        System.out.printf("Valor: %3s - Y: %4s - W: %4s - H: %4s%n",valor,rangoY,rangoW,rangoH);
    }
    

    Result:

    Valor:   1 - Y: null - W: null - H: null
    Valor:   2 - Y: null - W: null - H: null
    Valor:   3 - Y: null - W: null - H: null
    Valor:  20 - Y:    4 - W:    2 - H:    4
    Valor:  10 - Y:    2 - W:    1 - H:    5
    Valor:   4 - Y: null - W: null - H: null
    Valor:   5 - Y:   10 - W:   20 - H:    5
    

    Demo: link

  • answered by 13.02.2018 в 16:04