My question is related to regular expressions in Java [closed]

3

my problem comes with the following code:

private void _loadInfo(String sFile){

    InfoCategory categoria = null;
    String linea;
    String regex = "^\[\d{3}(-\d{4}){1,2}\]$"; // [XXX-XXXX] o [XXX-XXXX-XXXX] 
    String codCategoria = null;
    String nomCategoria = null;
    String idCategoria = null;
    String[] cadena = null;
    int cont = 0;

    try{

          f = new FileReader(sFile); // cargamos el fichero
          b = new BufferedReader(f); //leemos el fichero

          //primero hay que detectar que hemos encontrado una categoría

          linea = b.readLine();
          while(linea != null){ // cuerpo principal del autómata

              //hay que ignorar las líneas en blanco
              if(!linea.isEmpty()){ //si la linea NO esta en blanco
                  //aqui procesamos las categorias
                  if(linea.matches(regex)){ // si es una categoria, FALLO, NO SE POR QUÉ NO MTE LA PRIMERA CATEGORIA [018-0001]
                         codCategoria = linea;

                     }else if(linea.contains("name")){  
                         cadena = linea.split("="); // problema: me guarda el espacio incluido, para solucionar en el split poner "= "
                         nomCategoria = cadena[1];

                     }else if(linea.contains("id")){
                         cadena = linea.split("=");
                         idCategoria = cadena[1];
                     }

                  if(codCategoria != null && nomCategoria != null && idCategoria != null){ // significa que ya  tenemos una categoria con todas sus propiedades
                      InfoCategory cat = new InfoCategory(codCategoria,nomCategoria,idCategoria);
                      hCategories.put(codCategoria, cat); // lo metemos en el mapa
                      cont++;
                      // Una vez terminado ponemos todo otra vez a null para "limpiar" los valores de las variables
                      codCategoria = null;
                      nomCategoria = null;
                      idCategoria = null;
                  }

              }


              linea = b.readLine(); //lee la sig linea para procesar

          }
            System.out.println("Hemos metido "+cont+" categorias");                  
          b.close();
    }catch(Exception e){
        System.out.println("Ha ocurrido algun error con el archivo");
    }


}

For you to understand me, this function is to take a file (which I will later give you as it is that file), and put it on a map when you have processed a category (a category is considered processed if the fields have been entered of name, id, and the code of the category [xxx-xxxx]). The problem is that, in the file there are a total of 186 categories, but my program only catches 185 categories. The missing category I realized when I debugged the program. And it turns out that it is in the first iteration of all (that of the while loop (line! = Null)) when it catches the first category code, specifically, the [018-0001], I do not know why but it does NOT enter the condition of the if (linea.matches (regex)), when in the rest of the category codes it enters, that is, in the first code [018-0001] DOES NOT ENTER, but in the following code it is [018-0002] YES enter and so for the rest. The truth is that I do not understand it because it is the first time that fails. In the first image is there, with the variable line to that value is where it does not enter. Then in the second image is the IF where it DOES NOT ENTER the first time. And the last image is the structure of the file I'm reading.

I hope to have explained myself well, and what the function does is that, to catch text lines of the file and process them as I have put in the code. Thanks in advance

    
asked by victor26567 10.02.2018 в 13:16
source

1 answer

-1

This expression meets the requirements you describe in the question

^[\[]{1}[\d]{3}(-[\d]{4}){1,2}[\]]{1}$

You can try it online with regexplanet to see that it meets the requirements you want, yet check that you do not have any character of space, because if not, it will not fulfill the expression, for it you can use the function 'trim '.

    
answered by 10.02.2018 в 14:08