Convert string into array format into an array and retrieve values

1

In the database I have a TEXT where an array is saved in this format:

[ 
    [ resumo: null ] 
    [ datainicio: 2015-09-17T00:00:00.000-0300 ] 
    [ datafim: 2015-09-22T00:00:00.000-0300 ] 
    [ equipamento: 3421 ] 
]

I need to retrieve the values of the cache and datafim. How could I do that? I was researching on the split function of Java, but I could not use it in the right way to bring me the result.

    
asked by anonymous 22.09.2015 / 14:32

2 answers

2

Follows:


public static void main(String[] args)
    {
        String str = "[[ resumo: null ] [ datainicio: 2015-09-17T00:00:00.000-0300 ] [ datafim: 2015-09-22T00:00:00.000-0300 ] [ equipamento: 3421 ]]"
                .replaceAll(Pattern.quote("[["), "")    
                .replaceAll(Pattern.quote("]]"), "");

        String[] strArray = str.split(Pattern.quote("] ["));
        for (int i = 0; i menor strArray.length; i++)
            strArray[i] = strArray[i].replaceAll(Pattern.quote("] ["), "");

        for (String string : strArray)
            System.out.println(string);
    }
    
22.09.2015 / 15:25
1

Check if this is what you want:

(This code has not been tested but you can have an idea of how you can do it)

String frase = "[ 
    [ resumo: null ] 
    [ datainicio: 2015-09-17T00:00:00.000-0300 ] 
    [ datafim: 2015-09-22T00:00:00.000-0300 ] 
    [ equipamento: 3421 ] 
]";  
Pattern p = Pattern.compile("datainicio: (.*?) ]"); 
// para os outros casos deves de criar um Pattern idêntico  
// Pattern p = Pattern.compile("datafim: (.*?) ]"); 
// Pattern p = Pattern.compile("equipamento: (.*?) ]"); 

Matcher m = p.matcher(frase);  

while (m.find()) {  
    // chamada diferente:  
    System.out.println(m.group(1));  
} 

Here's a little more information on this

    
22.09.2015 / 14:50