Flo*_*ked 1 iphone objective-c
我有一个NSString与此内容:
<?xml version="1.0" encoding="UTF-8"?>
<rsp stat="ok">
<mediaid>y2q62</mediaid>
<mediaurl>http://twitpic.com/url</mediaurl>
</rsp>
Run Code Online (Sandbox Code Playgroud)
现在我想在没有所有其他字符串的情况下在新的NSString中获取twitpic-Url.我该怎么做?我可以在NSStrings中搜索吗?喜欢:找到字符串之间的字符串?或者我可以直接在NSString中找到URL吗?
谢谢你的帮助!
使用正则表达式:RegexKitLite
Here's a complete example using the HTTP matching URL from the RegexKitLite documentation. The RegexKitLite -componentsMatchedByRegex: method will return a NSArray of all the URL matches it finds in the string.
#import <Foundation/Foundation.h>
#import "RegexKitLite.h"
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *stringToSearch =
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@"<rsp stat=\"ok\">\n"
@" <mediaid>y2q62</mediaid>\n"
@" <mediaurl>http://twitpic.com/url</mediaurl>\n"
@"</rsp>\n";
NSString *urlRegex = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?";
NSArray *matchedURLsArray = [stringToSearch componentsMatchedByRegex:urlRegex];
NSLog(@"matchedURLsArray: %@", matchedURLsArray);
[pool release];
pool = NULL;
return(0);
}
Run Code Online (Sandbox Code Playgroud)
Compile and run with:
shell% gcc -arch i386 -o url url.m RegexKitLite.m -framework Foundation -licucore
shell% ./url
2010-01-14 16:05:32.874 url[71582:903] matchedURLsArray: (
"http://twitpic.com/url"
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2251 次 |
| 最近记录: |