1
[
Dobj(id=null, dmetaD=DmetaD(id=2068, embedded=true, size=123, comment=raghu, name=string, type=pdf)),dcont=DConD(data=abc)),
Dobj(id=null, dmetaD=DmetaD(id=2069, embedded=true, size=123, comment=raghu, name=string, type=pdf)),dcont=DConD(data=abc))
]

As you can see in the above object array i want to split and retrieve all the objects starting with the name DmetaD and DConD as string .

example:

String x=DmetaD(id=2068, embedded=true, size=123, comment=raghu, name=string, type=pdf))

String y=DConD(data=abc)
3
  • Can you tell us more about the source of this data? Wouldn't it be easier to extract data from the original Java objects themselves? Commented May 15, 2018 at 14:24
  • This is transported as a string from producer to subscriber in queue which is basically a list converted to string. Commented May 15, 2018 at 14:27
  • Well I would vote for converting back into a list on the other end and then using normal Java to extract the information you want. Commented May 15, 2018 at 14:30

1 Answer 1

1

You can use Pattern & Matcher with this regex (DmetaD\\(.*?\\)|DConD\\(.*?\\)) for example If you are using Java 9+ :

String input = "...";
String regex = "(DmetaD\\(.*?\\)|DConD\\(.*?\\))";
List<String> result = Pattern.compile(regex)
        .matcher(input)
        .results()
        .map(MatchResult::group)
        .collect(Collectors.toList());

Output

DmetaD(id=2068, embedded=true, size=123, comment=raghu, name=string, type=pdf)
DConD(data=abc)
DmetaD(id=2069, embedded=true, size=123, comment=raghu, name=string, type=pdf)
DConD(data=abc)

Before Java 9 you can use :

Matcher matcher = Pattern.compile(regex).matcher(input);

List<String> result = new ArrayList<>();
while (matcher.find()) {
    result.add(matcher.group());
}

Details about the regex (DmetaD\(.*?\)|DConD\(.*?\))

  • DmetaD\(.*?\) a matches which start with DmetaD followed by any thing between parenthesis.
  • | or
  • DConD\(.*?\) a matches which start with DConD followed by any thing between parenthesis.
Sign up to request clarification or add additional context in comments.

4 Comments

Just getting a compilation error The method results() is undefined for the type Matcher
It seems you are not using Java 9+ try the second solution @Prash
,please take a look at this json and can you please help me to get dmetaD and dCont. github.com/javayp/stream-zipkin/blob/master/client-appd.json
@Prash I strongly suggest to use a JSon parser instead of regex, there are many ways to parse a JSon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.