开发者

Simple string pattern matching

开发者 https://www.devze.com 2022-12-22 21:12 出处:网络
What\'s the easiest way of doing simple pattern matching a la .something.com something.com/ something.com/somefolder/*.jpg in the iPhone SDK? At this point it looks like I\'m going to have to use a re

What's the easiest way of doing simple pattern matching a la .something.com something.com/ something.com/somefolder/*.jpg in the iPhone SDK? At this point it looks like I'm going to have to use a regular expression library of some sort, which is really overkill when all I need is simple wildcard matching. None of the standard NSStri开发者_如何学编程ng methods seem to be available on the iPhone.


For the equivalent of a matching expression with a single wildcard (*), you can use NSString's hasPrefix: & hasSuffix:.

if ([string hasPrefix: @"something.com/somefolder/"] 
     && [string hasSuffix: @".jpg"])
{
   // string matches pattern: "something.com/somefolder/*.jpg"
}


Go the regular expression route: RegexKitLite / RegexKitLite-3.3.tar.bz2. It's not an overkill, this is exactly the kind of problem that regular expressions are good at.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号