java - Regular expression to parse id -
java - Regular expression to parse id -
i'm looking regular look parse id of record in mongodb:
{"$oid":"5527b117d3d511091e1735e2"} i'm trying next 1 fails:
private static final pattern p = pattern.compile("\\{\"([a-za-z\\d]+)\"\\}"); matcher m = p.matcher("{\"$oid\":\"5527b117d3d511091e1735e2\"}"); if(!m.find()) { throw new illegalargumentexception("the id should within parenthesis , quotes."); } any help ?
you need include key part in regex or "\\{\"([a-za-z\\d$]+)\":" because [a-za-z\\d]+ won't match inbetween : , there isn't closing curly brace next key part.
final pattern p = pattern.compile("\\{\"([a-za-z\\d$]+)\":\"([^\"]*)\"\\}"); matcher m = p.matcher("{\"$oid\":\"5527b117d3d511091e1735e2\"}"); if(m.find()) { system.out.println("key : " + m.group(1)); system.out.println("value : " + m.group(2)); } output:
key : $oid value : 5527b117d3d511091e1735e2 java regex
Comments
Post a Comment