Skip to main content
Removed off-topic question
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Is it good way to parse Parsing complex string data like follow in java

I have a complex strings like the following.:

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than ithen I prepare objects out of them as follows:

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way  ? Also is there a way where i can compare my string with some template and throw exception if the str is not complaint with the template.

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}

Is it good way to parse string data like follow in java

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way  ? Also is there a way where i can compare my string with some template and throw exception if the str is not complaint with the template.

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}

Parsing complex string data

I have a complex strings like the following:

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and then I prepare objects out of them as follows:

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way?

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}
added 136 characters in body
Source Link

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way ? Also is there a way where i can compare my string with some template and throw exception if the str is not complaint with the template.

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way ?

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way ? Also is there a way where i can compare my string with some template and throw exception if the str is not complaint with the template.

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}
deleted 1 character in body
Source Link

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way ?

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)");.matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way ?

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)");.matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}

I have a complex strings like following.

$company=>Company(code->MyCompany)
$brand=>Brand(code->Brand_1, company->$company)

and than i prepare objects out of them as follows

Macro [key=$company, type=Company, map={code=MyCompany}]
Macro [key=$brand, type=Brand, map={ company=$company, code=Brand_1}]

using the following code for parsing. Is there a better way ?

public class Macro {

    private String key;

    private String type;

    private Map<String, String> map;
}

public static Macro getMacro(String str) {
    final Macro macro = new Macro();
    final Map<String, String> map = new HashMap<String, String>();
    final Matcher matcher = Pattern.compile("\\(([^)]+)\\)").matcher(str);
    while (matcher.find()) {
        final List<String> list = Arrays.asList(matcher.group(1).split(",")).stream()
                .collect(Collectors.toList());
        list.stream().forEach(a ->
        {
            final String[] ar = a.split("->");
            map.put(ar[0], ar[1]);
        });
    }
    str = str.replaceAll("\\(.*\\)", "");
    final String[] s = str.split("=>");
    macro.setMap(map);
    macro.setKey(s[0]);
    macro.setType(s[1]);
    return macro;
}
Source Link
Loading