1

I'm trying to find how to extract "2013-05-01/game1" from "/games/usa/2013-05-01/game1.html", but I don't how to do that. I already tried some Regex on it but it didn't work. And I also don't know if this would be the best way. This is what I have tried so far.

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"/[0-9]{4}-[0-9]{2}-[0-9]-{2}\//\.*$)" options:NSRegularExpressionCaseInsensitive error:NULL];
NSTextCheckingResult *newSearchString = [regex firstMatchInString:opening_time options:0 range:NSMakeRange(0, [opening_time length])];
NSString *substr = [opening_time substringWithRange:newSearchString.range];
NSLog(@"%@", substr);

Any help would be appreciated.

2
  • 1
    Can you rely on the format surrounding the string? Will it always be like your example? Commented Jul 20, 2013 at 5:23
  • yes, always like that! Commented Jul 20, 2013 at 15:05

2 Answers 2

4

One way would be to use NSString's componentsSeparatedByString: method. Here's an example

NSString *str = [@"/games/usa/2013-05-01/game1.html" stringByDeletingPathExtension];
NSArray *components = [str componentsSeparatedByString:@"/"];
NSString *result = [NSString stringWithFormat:@"%@/%@", [components objectAtIndex:3], [components objectAtIndex:4]];
NSLog(@"%@", result);

Of course, this would rely on the fact that the format never changes, and this most likely won't work if it does change.

Sign up to request clarification or add additional context in comments.

1 Comment

Regex is often overkill for 90% of cases when it is used. Good on you for pointing out a straightforward solution.
2

try this:

@"[0-9]{4}-[0-9]{2}-[0-9]{2}/[^.]+"

or this:

@"[0-9]{4}-[0-9]{2}-[0-9]{2}/.+?(?=\\.html)"

1 Comment

That works too, but i'll keep the other one as the answer based on what @hd1 said! But thanks!

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.