-2

I have two strings as follows:

  • "activityTypes": {"activityType": "COLLECTIONS"},
  • "activityTypes": {"activityType": "E-DROP"},

Which I need to convert to -

  • "activityTypes": [{"activityType": "COLLECTIONS"}],
  • "activityTypes": [{"activityType": "E-DROP"}],

What would be the regular expression in java for the above transformation?

Gajanand Jha
  • 149
  • 5
  • 1
    If the strings really are as in the contrived example, you could use `input = input.replace("{", "[{").replace("}", "}]")` – Tim Biegeleisen Oct 31 '16 at 13:34
  • The problem is that this is just a part of a bigger string. For simplicity, I only posted the relevant section. So, we can't use simple java replace function as it would replace all those occurrences of '{' and '}' which shouldn't be replaced. I need something like `(.*)"activityTypes": {(.*)},(.*)` – Gajanand Jha Oct 31 '16 at 13:37
  • You can use find and replace with capturing groups http://stackoverflow.com/questions/1277157/java-regex-replace-with-capturing-group?rq=1 – yeugeniuss Oct 31 '16 at 14:30

2 Answers2

0

You can get your expected output by using REGEX or by processing JSON.

Assuming you want to use this code for Mongodb using Mongodb-java-driver-3.0.1

    BasicDBObject myobj = new BasicDBObject();
    BasicDBObject myobj1 = new BasicDBObject().append("activityType", "COLLECTIONS");
    myobj.append("activityTypes", myobj1);
    System.out.println(myobj);
    List<BasicDBObject> myarray = new ArrayList<>();
    myarray.add(myobj1);
    myobj.put("activityTypes", myarray);
    System.out.println(myobj);

this will give output as

{ "activityTypes" : { "activityType" : "COLLECTIONS"}}
{ "activityTypes" : [ { "activityType" : "COLLECTIONS"}]}
Belhe001
  • 89
  • 9
0

There is probably a better way.

public static void main(String[] args) {
        // TODO code application logic here
        String string1 = "\"activityTypes\": {\"activityType\": \"COLLECTIONS\"} "
                + "\n \"activityTypes\": {\"activityType\": \"E-DROP\"},";

        Scanner input = new Scanner(string1);

        Pattern pattern = Pattern.compile("\\\"activityTypes\\\": \\{[\\s\\S]+?\\}");



        while(input.hasNext())
        {
            Matcher m = pattern.matcher(input.nextLine());
            if(m.find())
            {
                string1 = m.group().replace("{", "{[");                                 
                System.out.println(string1);
            }
            else
            {
                System.out.println("Nothing found!");
            }
        }
        input = new Scanner(string1);
        pattern = Pattern.compile("\\\"activityTypes\\\": \\{\\[[\\s\\S]+?\\}");

         while(input.hasNext())
        {
            Matcher m = pattern.matcher(input.nextLine());
            if(m.find())
            {
                string1 = m.group().replace("}", "]}");                                 
                System.out.println(string1);
            }
            else
            {
                System.out.println("Nothing found!");
            }
        }       
    }
SedJ601
  • 12,173
  • 3
  • 41
  • 59