0

I need to extract a param and the value for that param from a string ((created_date{[1976-03-06T23:59:59.999Z TO *]}|1)). Here param is created_date. Value is 1976-03-06T23:59:59.999Z TO * where * denotes no restriction. I need to extract the data as shown below i,e it should be an array of string.

created_date
1976-03-06T23:59:59.999Z
*
1

I have tried some online regex tool to find a suitable regex and also tried some code on trial and error basis.

String str = "((created_date{[1976-03-06T23:59:59.999Z TO *]}|1))";
String patt = "\\((.*)\\{(.*)\\}\\|(1|0)\\)";
Pattern p = Pattern.compile(patt);
Matcher m = p.matcher(str);
MatchResult result = m.toMatchResult();
System.out.println(result.group(1));

similary result.group(2) and 3.. depending on the result.groupCount().

I need to extract the data as shown below i,e it should be an array of string.

created_date

1976-03-06T23:59:59.999Z

*

1
0

1 Answer 1

1

You can use the following :

String str = "((created_date{[1976-03-06T23:59:59.999Z TO *]}|1))";
String patt = "\\(\\(([^{]+)\\{\\[([^ ]+) TO ([^]]+)]}\\|([01])\\)\\)";
Pattern p = Pattern.compile(patt);
Matcher m = p.matcher(str);
if (m.matches()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
    System.out.println(m.group(3));
    System.out.println(m.group(4));
}

Try it here !

Note that you need to invoke a Matcher's find(), matches() or more rarely lookingAt() before you can use most of its other methods, including the toMatchResult() you were trying to use.

Sign up to request clarification or add additional context in comments.

Comments

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.