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

Di Wu
  • 6,436
  • 3
  • 35
  • 51

4 Answers4

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);
Paul.s
  • 38,494
  • 5
  • 70
  • 88
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*))(&.*?=.*)*
mmdemirbas
  • 9,060
  • 5
  • 45
  • 53
  • Why such a strict regex? Surely something like `@".*id=(\\d*)"` would be adequate? – Paul.s Aug 28 '12 at 14:46
  • @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](http://regexr.com?31vjm) `234`. – mmdemirbas Aug 28 '12 at 14:54
  • Ok - well then surely this is still much more succinct and now deals with that case `@".*&id=(\\d*)"` – Paul.s Aug 28 '12 at 14:55
  • 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. – mmdemirbas Aug 28 '12 at 15:08
  • Ok, `[^&]*&` can be simplified to `.*?&`. – mmdemirbas Aug 28 '12 at 15:16
  • How about `.*[&\?]id=(\d*).*` - BTW I'm just trying to remove some of the scary for people who aren't that used to regex's – Paul.s Aug 28 '12 at 15:30
  • It matches an invalid URL like `http://www.example.com/&id=1893736`. If all URLs are assumed valid, this is acceptable. – mmdemirbas Aug 29 '12 at 06:24
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.

Community
  • 1
  • 1
Jesper
  • 7,477
  • 4
  • 40
  • 57
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).

J2theC
  • 4,412
  • 1
  • 12
  • 14