vendredi 16 juin 2017

extarct data using pattern matching in java

I have requriement in java to extarct data from the big string using pattern compile.

INPUT STRING : [{"KEY1" :"VALUE1","KEY2" :"VALUE2","KEY3" :"VALUE3","KEY4" :"VALUE4"},{"KEY1" :"VALUE5","KEY2" :"VALUE6","KEY3" :"VALUE7","KEY4" :"VALUE8"}]

OUTPUT : set of 2 string first : {"KEY1" :"VALUE1","KEY2" :"VALUE2","KEY3" :"VALUE3","KEY4" :"VALUE4"} second : {"KEY1" :"VALUE5","KEY2" :"VALUE6","KEY3" :"VALUE7","KEY4" :"VALUE8"}

I am trying below code :

ArrayList token_data = new ArrayList();

        String myString = "[{"KEY1" :"VALUE1","KEY2" :"VALUE2","KEY3" :"VALUE3","KEY4" :"VALUE4"},{"KEY1" :"VALUE5","KEY2" :"VALUE6","KEY3" :"VALUE7","KEY4" :"VALUE8"}]"
        System.out.println(myString);
        Pattern p1 = Pattern.compile("\"([^\"]+)\"");
        Matcher m = p1.matcher(myString);

        while(m.find())
        {

            System.out.println(m.group(1)); 
            //m.group(1);
        token_data.add(m.group(1));
        }


    }

    else{
        token_data.add("NA");
    }

Aucun commentaire:

Enregistrer un commentaire