2

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..

Vincent Dagpin
  • 3,581
  • 13
  • 55
  • 85
  • 1
    Why can't you deserialize the JSON into an object instead? – Oded Apr 19 '12 at 13:35
  • Why do you parse it yourself? Use a JSON parser and stop worriing!? – Argeman Apr 19 '12 at 13:35
  • Might I suggest http://json.codeplex.com/ which has done all the work for you already :) – Justin Pihony Apr 19 '12 at 13:36
  • im using that method Newtonsoft.Json but i will return a problem upon running on Windows XP.. it's ok if from windows 7 – Vincent Dagpin Apr 19 '12 at 13:36
  • 1
    What problem? That is probably more your issue than needing a regex. – Justin Pihony Apr 19 '12 at 13:37
  • Newtonsoft.Json Error: Method not found: 'Boolean.System.Runtime.Serialization.DataContractAttribute.get_IsReference()' i think regex would be an alternative – Vincent Dagpin Apr 19 '12 at 13:41
  • 2
    http://stackoverflow.com/questions/6076310/correct-serializeobject-method-run describes your actual problem and how to fix it. ***Don't use regex. It's insanity.*** ... just install the right .net servicepack for your machine. – spender Apr 19 '12 at 13:51
  • That's not actually JSON, though. There should be quotes around keys, and string values. – moveaway00 Jun 12 '15 at 21:12

2 Answers2

1

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 + "\"+ *: *\"+).*(?=\")");

ShawnFeatherly
  • 2,470
  • 27
  • 20
  • 1
    If you try to select the value of a key anywhere in the json besides in the last element, this will take all the string after the match `.*`. If you know what character classes to expect you can use `[a-zAZ]*` instead. – adrien Feb 01 '21 at 13:40
0

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.

Community
  • 1
  • 1
Zac B
  • 3,796
  • 3
  • 35
  • 52
  • Try browsing the links/suggestions on this question, then: http://stackoverflow.com/questions/4427473/how-to-parse-the-json-array-value-in-c-sharp-windows-phone-7 – Zac B Apr 24 '12 at 15:33