← Back Home

Regex Key Value on Java

  1. til

Today, one intern on my team found valuable thing. Given text, get some values and remove noises.

Sample text {"node"=>"8.11","java"=>"1.8"} and should retrieve key and value. Expected result

node 8.11
java 1.8

Using java, it can be solved by this code

    String regex = "\"(?<key>[.[^\"]]+)\"=>\"(?<value>[.[^\"]]+)\"";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher("{\"node\"=>\"8.11\",\"java\"=>\"1.8\"}");
    while (matcher.find()) {
      System.out.println(matcher.group("key") + " " + matcher.group("value"));
    }
comments powered by Disqus