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.

8
  • It hardly thepends on witch character are admittend inside the "property names" and in "values" Commented Nov 18, 2015 at 15:04
  • 1
    Language? JavaScript I suppose. Why a regex? Commented Nov 18, 2015 at 15:04
  • 3
    Decode the JSON into whatever native array/object type your language of choice has and simply access the key...!? Commented Nov 18, 2015 at 15:04
  • See Python demo (just to show how easy it is with a JSON parser). Commented Nov 18, 2015 at 15:05
  • 2
    stackoverflow.com/q/26420725/476?! Commented Nov 18, 2015 at 15:13

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
See the SO post I linked. You need 'tokens' instead of 'match'
-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.

2 Comments

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.
"Log file case" it is your case. In this question does not means log file

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.