0

I need to parse a URL in the following format:

http://www.example.com/?method=example.method&firstKey=firstValue&id=1893736&thirdKey=thirdValue

All I need is the value of 1893736 within &id=1893736.

I need to do the parsing in Objective-C for my iPhone project. I understand it must have something to do with regular expression. But I just have no clue how to do it.

Any suggestions would be appreciated. :)

3
  • URl is not opening correctly... Commented Aug 28, 2012 at 14:11
  • Because, it is just an example. Commented Aug 28, 2012 at 14:12
  • for what reason you need regex? you can do it with simple functions like lastIndexOf, Substr and Split Commented Aug 28, 2012 at 14:12

4 Answers 4

1

You don't need a regex for this. You can try something like this

NSString *url = @"http://www.example.com/?method=example.method&firstKey=firstValue&id=1893736&thirdKey=thirdValue";

NSString *identifier = nil;

for (NSString *arg in [[[url pathComponents] lastObject] componentsSeparatedByString:@"&"]) {
  if ([arg hasPrefix:@"id="]) {
    identifier = [arg stringByReplacingOccurrencesOfString:@"id=" withString:@""];
  }
}

NSLog(@"%@", identifier);
Sign up to request clarification or add additional context in comments.

Comments

1

Don't use regular expressions. Use NSURL to reliably extract the query string and then use this answer's code to parse the query string.

Comments

1

Use this:

.*/\?(?:\w*=[^&]*&)*?(?:id=([^&]*))(?:&\w*=[^&]*)*

And grap first group: \1. You will obtain 1893736.

Simplifying

If the id can consist of only digits:

.*/\?(?:\w*=[^&]*&)*?(?:id=(\d*))(?:&\w*=[^&]*)*

If you don't care about capturing uninterested groups (use \3 or id in this case):

.*/\?(\w*=.*?&)*?(id=(?<id>\d*))(&\w*=.*)*

More simpler version (use \3):

.*/\?(.*?=.*?&)*(id=(\d*))(&.*?=.*)*

7 Comments

Why such a strict regex? Surely something like @".*id=(\\d*)" would be adequate?
@Paul.s To ensure it doesn't match other parameters. For example: In a URL such as http://www.example.com/?pid=1893736&id=123&cid=234 the regex you suggested will match 234.
Ok - well then surely this is still much more succinct and now deals with that case @".*&id=(\\d*)"
No, an ampersand before id not necessary: http://www.example.com/?id=1893736. But, if OP approves that the id can consist of only digits, then replacing id=([^&]*) with id=(\d*) simplifies my regex, as you suggested.
Ok, [^&]*& can be simplified to .*?&.
|
0

Instead of using regex, you can split the string representation of your NSURL instance. In your case, you can split the string by the appersand (&), loop the array looking for the prefix (id=), and get the substring from the index 2 (which is where the = ends).

Comments

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.