【发布时间】:2013-06-19 20:36:16
【问题描述】:
我正在尝试使用正则表达式来匹配前面有某些单词的文本(在这种情况下是“幻灯片”和“标题”)。我一直在尝试使用后视断言并将它们相互嵌套,但它似乎不起作用。另一个问题是我用来匹配的“幻灯片”和“标题”词之间还有一些其他文本。
我试过了:
(?<=slide(?<=title\\s=\\s)).*(?=\\\")
(?<=title\\s=\\s(?<=slide)).*(?=\\\")
关于如何做到这一点的任何建议?额外的斜线用于转义;我在objective-c中使用它,但我不知道这是否重要。
我正在抓取 JSON 的一部分以获得更好的上下文(我希望在每张幻灯片中的“标题”之后获取标题):
slide = {
createdAt = "2013-06-18T20:06:50Z";
description = "<p>Due to the amount of attention paid to each organization's top prospects and early-round draft picks, many of the game's underrated prospects are perpetually obscured. Most of the time, these prospects are younger players who are housed in the low minors and still require considerable physical projection. At the same time, there are countless prospects on the older side of the age curve who have dipped off the radar due to injury.</p><p>Here's a look at one \"hidden gem\" from each organization who could make a push for the major leagues in the coming years.</p>";
embedCode = "";
externalId = "<null>";
id = 3219926;
photoHasCropExact = 1;
photoHasCropNorth = 0;
primaryPhoto = {
url = "http://img.bleacherreport.net/img/slides/photos/003/219/926/hi-res-5382332_crop_north.jpg";
};
title = "Each MLB Team's 'Hidden Gem' Prospect Fans May Not Know About";
updatedAt = "2013-06-18T22:26:30Z";
url = "<null>";
【问题讨论】:
-
你想用正则表达式解析 JSON 吗?一个合适的 JSON 解析器,例如 NSJSONSerialization 将是一个更好的解决方案。顺便说一句。您在问题中显示的不是 JSON。 - 也许你可以更好地解释你真正想要达到的目标。
-
已修复,非常感谢。刚刚结束使用 NSJSONSerialization 代替感谢您的帮助。
标签: objective-c regex json screen-scraping