2

Hi i'm new to regex and i'm trying to grab the value after l_fix in the following json:

{ "id": "626307" ,"t" : ".INX" ,"e" : "INDEXSP" ,"l" : "2,050.59" ,"l_fix" : "2050.59" ,"l_cur" : "2,050.59" ,"s": "0" ,"ltt":"3:08PM EST" ,"lt" : "Nov 17, 3:08PM EST" ,"lt_dts" : "2015-11-17T15:08:33Z" ,"c" : "-2.60" ,"c_fix" : "-2.60" ,"cp" : "-0.13" ,"cp_fix" : "-0.13" ,"ccol" : "chr" ,"pcls_fix" : "2053.19" }

right now i'm using \d[,](?:\d*\.)?\d+ however this seems a bit brute force and i'm sure it can be done better.

Jon Surrell
  • 9,444
  • 8
  • 48
  • 54
Jason
  • 165
  • 2
  • 6
  • It hardly thepends on witch character are admittend inside the "property names" and in "values" – RiccardoC Nov 18 '15 at 15:04
  • 1
    Language? JavaScript I suppose. Why a regex? – Diego Nov 18 '15 at 15:04
  • 3
    ***Decode*** the JSON into whatever native array/object type your language of choice has and simply access the key...!? – deceze Nov 18 '15 at 15:04
  • See [Python demo](http://ideone.com/7QzW8l) (just to show how easy it is with a JSON parser). – Wiktor Stribiżew Nov 18 '15 at 15:05
  • I agree with @deceze. Parsing the json with a fitting library (depends on the language you're working in) is the proper way to do this. – steinar Nov 18 '15 at 15:06
  • You're probably asking the wrong question. You're trying to deal with a string of JSON? As @deceze mentions, your best bet is probably to parse the JSON string, and then just access the value by key. – Jon Surrell Nov 18 '15 at 15:07
  • i'm using matlab and the json parser is slow. since i always only want the value after `l_fix` i figure a regex would be a faster way to grab the data i want. – Jason Nov 18 '15 at 15:10
  • 2
    → http://stackoverflow.com/q/26420725/476?! – deceze Nov 18 '15 at 15:13

2 Answers2

4

A regex doesn't seem to be what you need, however if you want to do it with a regex it could be this one: "l_fix"\s*:\s*"(.+?)". Then, in the first group you'll have the value you are looking for.

See it working here

Also, see this SO post about regex groups in matlab.

Diego
  • 16,436
  • 26
  • 84
  • 136
  • I had tried this also but i only want to return the number after `l_fix` and not the key itself. I thought that `?:` would do the trick but it doesn't seem to make a difference. – Jason Nov 18 '15 at 15:37
  • See the SO post I linked. You need `'tokens'` instead of `'match'` – Diego Nov 18 '15 at 15:44
-3

Why regex, best way using JSON parser. Example in JavaScript:

var str = '{ "id": "626307" ,"t" : ".INX" ,"e" : "INDEXSP" ,"l" : "2,050.59" ,"l_fix" : "2050.59" ,"l_cur" : "2,050.59" ,"s": "0" ,"ltt":"3:08PM EST" ,"lt" : "Nov 17, 3:08PM EST" ,"lt_dts" : "2015-11-17T15:08:33Z" ,"c" : "-2.60" ,"c_fix" : "-2.60" ,"cp" : "-0.13" ,"cp_fix" : "-0.13" ,"ccol" : "chr" ,"pcls_fix" : "2053.19" }';
var parsedStr = JSON.parse(str);
alert(parsedStr.l_fix);

Other languages also has JSON parser.

Ramin Darvishov
  • 1,043
  • 1
  • 15
  • 30
  • bad answer; in my case I'm trying to parse log files. I don't want to load up each line of the log, parse the JSON and then find the data I need, it's time-consuming and memory intensive to do that. Using a regex seems possible to do and it is *faster* and uses *less memory*. –  Nov 23 '17 at 21:08
  • "Log file case" it is your case. In this question does not means log file – Ramin Darvishov Nov 25 '17 at 20:01