2

I have a string like this:

*********** name: NOTINSTATE timestamp: 2015-09-16T12:33:01.253Z 
MyKeyValue1 = myData MyKeyValue2 = 0.0 based on filter: no Filter
********************************

In this String i have the KeyValuePairs:

"name" NOTINSTATE
"timestamp" 2015-09-16T12:33:01.253Z
"MyKeyValue1" myData
"MyKeyValue2" 0.0
"based on filter" no Filter

I was thinking about something like Freemarker in reverse way but i don't think that Freemarker others this functionality.

I know i could do it on a dirty way and work with pattern and split the String but there must be a better way to do this.

Any suggestions or frameworks which would be useful? My searchString itself will not be changed in the future. It will be always the same.

3
  • The values are obviously variable, but are the keys variable? All of them? Commented Sep 17, 2015 at 16:19
  • Nope the keys are always the same. Commented Sep 17, 2015 at 16:21
  • I was think about using some regex where i can say what is between two patterns to get my value. But i would have to do it for very value. This would be the dirty way. Commented Sep 17, 2015 at 16:24

1 Answer 1

2

A regular expression is your friend:

String input = "*********** name: NOTINSTATE timestamp: 2015-09-16T12:33:01.253Z\n" +
               "MyKeyValue1 = myData MyKeyValue2 = 0.0 based on filter: no Filter\n" +
               "********************************";
String regex = "\\*+\\s+" +
               "(name):\\s+(.*?)\\s+" +
               "(timestamp):\\s+(.*?)\\s*[\r\n]+" +
               "(MyKeyValue1)\\s+=\\s+([^=]*)\\s+" +
               "(MyKeyValue2)\\s+=\\s+([^=]*)\\s+" +
               "(based on filter):\\s+(.*?)\\s*[\r\n]+" +
               "\\*+";
Matcher m = Pattern.compile(regex).matcher(input);
if (m.matches()) {
    Map<String, String> pairs = new LinkedHashMap<>();
    for (int i = 1; i <= 10; i += 2)
        pairs.put(m.group(i), m.group(i + 1));

    // print for testing
    for (Entry<String, String> entry : pairs.entrySet())
        System.out.printf("\"%s\" %s%n", entry.getKey(), entry.getValue());
}

Output is exactly what you showed:

"name" NOTINSTATE
"timestamp" 2015-09-16T12:33:01.253Z
"MyKeyValue1" myData
"MyKeyValue2" 0.0
"based on filter" no Filter

Update

The above regex is lenient on spaces, but strict on key names. You could go strict on spaces and lenient on key names, or any other combination:

String regex = "\\*+ " +
               "(\\w+): (.+?) " +
               "(\\w+): (.+?)[\r\n]+" +
               "(\\w+) = ([^=]+?) " +
               "(\\w+) = ([^=]+?) " +
               "([^:]+): (.+?)[\r\n]+" +
               "\\*+";
Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking about regex but not a regex string to get all the value's. I have to do more with regex.

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.