1

I have a string like this

"Settings" : { "UserId" : 3, "UserToken" : "4578965235874158", "SecurityCode" : "111", "Password" : "12345", "UserPhone" : "555-555-1531", "drop" : -1, "UserLastName" : "Smith" }

I need a regular expression pattern to find the value of the "UserToken"( in this example 4578965235874158)

Sorry if this is a "Give me code" question, but I never worked with regular expressions and I do not have much time to learn and try to do it myself.

Thanks for help.

Additional notes: I will use it in objective-C for an iPhone application. I don't know if that affect anything but just in case.

EDIT' The pattern is: the key is always "UserToken" followed by a space then by a : then another space after that comes the value inside double Quotes ". I would like to get this value.

6
  • Describe a pattern for the token. For example, "exactly n digits" etc. Commented Mar 4, 2013 at 11:08
  • does it need to be regex? JSON values can be accessed by their key. You just need an objective-c json parser Commented Mar 4, 2013 at 11:10
  • 1
    Has the json always the same format? In that case I'd rather use a JSON parser than regex magic. Commented Mar 4, 2013 at 11:10
  • @AndrewLogvinov I edit my question with the pattern. Commented Mar 4, 2013 at 11:16
  • @Viehzeug I need to use regex because I dont know where the "settings" key is located inside the Json. It might be at the root level or inside another key. and I am trying to avoid looping on all the key and inner keys Commented Mar 4, 2013 at 11:17

2 Answers 2

0

Since it's a json format you don't really need to parse it with regex, but if you need to, this can be a solution:

(?<="UserToken"\s:\s")\d+
Sign up to request clarification or add additional context in comments.

Comments

0

I would have converted it and access all information I need like:

NSString *str = @"\"Settings\" : { \"UserId\" : 3, \"UserToken\" : \"4578965235874158\", \"SecurityCode\" : \"111\", \"Password\" : \"12345\", \"UserPhone\" : \"555-555-1531\", \"drop\" : -1, \"UserLastName\" : \"Smith\" }";

//NSDictionary *jsonDict = [str JSONValue];

NSError *error;
NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

than all information are stored on the results NSDictionary, just have to do something like: [results valueForKey:(NSString *)] or [results objectForKey::(NSString *)]

2 Comments

Thanks for this but I need to use Regex
Welcome :) around here, we say thanks by upvotes :) perhaps if I I'll try to look & write you later a method using Regex (I don't have it on minde right now)

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.