我正在编写一个应用程序,它从网站获取一些 HTML。我有一个带有 HTML 的字符串,并且有一些我想从中获取信息的标签。但是,我需要使用通配符,因为标签的某些属性在每次请求后都会发生变化。我想立即从我需要的所有标签中获取信息,这样我就不需要在 HTML 中进行太多搜索。有人可以解释/指出有关 NSRegularExpression 模式和使用它的教程吗?我在网上、Stackoverflow 和 Apple 文档上查看过,但没有找到任何有用的信息。另外,如果有人知道我做同样事情的另一种方法,也请发帖。
I have an application that I'm writing, which gets some HTML from a website. I have a string with the HTML, and there are certain tags which I would like to get information from. However, I need to use wildcards, since certain attributes of the tag change after every request. I would like to get information from all tags that I need at once, so I don't need to search through the HTML too much. Can somebody please explain/point me to a tutorial on NSRegularExpression patterns and using it? I have looked online, on Stackoverflow, and on the Apple Documentation, and I didn't find anything helpful. Also, if somebody knows another way for me to do the same thing, please post as well.
发布评论
评论(2)
您可以改用 NSXMLParser 吗?我不确定它是否适合您的特定应用程序,但它只需要遍历 HTML 字符串一次。您可以简单地为每个标签添加一个“if”语句,因此实际上也不需要通配符。
如果您以前没有使用过 NSXMLParser,Event-驱动 XML 编程指南非常有帮助。
Could you use an NSXMLParser instead? I'm not sure if it would suit your specific application, but it would only require going through the HTML string once. You can simply have one "if" statement per tag, so wildcards aren't really needed either.
If you haven't used NSXMLParser before, the Event-Driven XML Programming Guide is very helpful.
我尝试在线使用 NSRegularExpression 文档,在了解了正则表达式模式的基础知识后,就更容易理解它了。如果您在线查看,您可以找到许多正则表达式模式说明,并且可在此处找到适用于 iOS 的 NSRegularExpression 文档: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
对于 OS X Lion:
http://developer.apple.com/库/mac/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html。
I tried using the NSRegularExpression documentation online, and after I understood the basics of regex patterns in general it was easier to understand it. If you look online you can find many regex pattern explanations, and the NSRegularExpression documentation is available here for iOS: http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html
and for OS X Lion:
http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSRegularExpression_Class/Reference/Reference.html.