Vincent Dagpin
Vincent Dagpin

Reputation: 3611

Regex for getting key:value from JSON in C#

what is the pattern for getting a-z, A-Z, 0-9, space, special characters to deteck url

This is my input string:

{id:1622415796,name:Vincent Dagpin,picture:https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/573992_1622415796_217083925_q.jpg}

This is the pattern: so far

([a-z_]+):[ ]?([\d\s\w]*(,|}))

Expected Result:

id:1622415796
name:Vincent Dagpin
picture:https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/573992_1622415796_217083925_q.jpg

the problem is i can't get the last part.. the picture url..

any help please..

Upvotes: 2

Views: 5224

Answers (2)

ShawnFeatherly
ShawnFeatherly

Reputation: 2638

If this is the only kind of json input you expect and further json parsing is very unlikely, a full json parser would be overkill.

A string split may be all you need, jsonString.Split(',', '{', '}'); The regex for that would be along the lines of [{},]([a-z_]+):[ ]?(.+?)(?=,|})

If you can modify the json string that's being sent, you can key the RegEx on something else, like double quotes. Here's one I'm using that requires knowing the json key name. System.Text.RegularExpressions.Regex("(?<=\"" + key + "\"+ *: *\"+).*(?=\")");

Upvotes: 2

Zac B
Zac B

Reputation: 4232

I don't think a regex is the right solution. C# already contains the tools you need in JavaScriptSerializer. Check out the answer here to see how.

Upvotes: 0

Related Questions