java regex match words that aren't numbers -


i have json missing quotes

{     data: [{         timestamp: 1467720920,         val: {             min: 6.90,             max: 7.25,             avg: 7.22         },         temp: {             min: 75.49,             max: 75.49,             avg: 75.49         },         gps: {             lat: 0.707581,             long: -1.941864,             hdop: 2.54,             ttf: 49.4         }     }],     id: a1000049a6248c,     groupid: hu5ppc1e,     rssi: -93,     cell: {         timestamp: 1467731669,         rssi: -93,         lat: 0.735554,         long: -1.974655     } } } 

i need put quotes around of words left of colon , of words aren't purely numbers right of colon. need quotes around a1000049a6248c not -1.974655. how make regex in java? i've tried

json.replaceall("(\\w+|[+-]([0-9]*[.])?[0-9]+)", "\"$1\"");

which put every word in quotes. i've tried word isn't numbers json.replaceall("\\b(?!\\d*)\\b", "\"$1\"");

expected format

{   "data": [     {       "timestamp": 1463494202,       "val": {         "min": 6.75,         "max": 7.19,         "avg": 7.14       },       "temp_int": {         "min": 54.28,         "max": 54.28,         "avg": 54.28       },       "gps": {         "lat": 0.711407,         "long": -1.460091,         "hdop": 1.42,         "ttf": 42       }     }   ],   "id": "a1000049a624d1",   "groupid": "299f7g5ar",   "rssi": -83,   "cell": {     "timestamp": 1463501353,     "rssi": -83,     "lat": 0,     "long": 0   } } 

you can try lookahead regex:

str = str.replaceall("[\\w-]+(?=\\s*:)", "\"$0\"")          .replceall("(?<=:)\\s*(?!-?\\d+(?:\\.\\d+)?\\s*(?:,|\\r?\\n))([\\w-]+)", "\"$1\""); 

regex demo

(?!-?\\d+(?:\\.\\d+)?\\s*(?:,|\\r?\\n)) negative lookahead assert we're not matching negative/positive decimal/integer number.


Comments