我试图操纵一个HTML块来获取某个子字符串之后的字符串(" bookmark">)。我将HTML内容转换为NSString
并使用componentsSeparatedByString("\"bookmark\">")
创建单独的数组,但之后我必须进入每个数组并再次执行以分隔每个匹配项。这有点乏味(加上我不确定我是否会以这种方式获得所有事件)。还有更直接的东西吗?或者是否有一个方法可以计算子串的使用次数,所以我可以想出一些循环来运行这么多次?请尽可能提供对Swift友好的答案。
下面是一个简化的例子,我想以这样的方式操纵字符串,以便我摆脱"不要"和"想要"结束"这就是我需要的"
这是其中的一部分。我希望能够以这样的方式操纵它来画出电影的标题(BROOKLYN和THE DANISH GIRL) -
> <li id="post-662129" class="post-662129 movie type-movie
> status-publish has-post-thumbnail hentry tag-brooklyn
> tag-domhnall-gleeson tag-emory-cohen tag-jim-broadbent
> tag-john-crowley tag-julie-walters tag-nick-hornby tag-saoirse-ronan"
> style='margin-right: 0%;'>
> <h2><a href="http://www.nitehawkcinema.com/movie/brooklyn/" title="Permalink to BROOKLYN" rel="bookmark">BROOKLYN</a></h2>
> <div class="first-col">
> <a href="http://www.nitehawkcinema.com/movie/brooklyn/" class="poster-image-small"><img
> src="/timthumb.php?src=/wp-content/uploads/2016/01/poster-43c98278-5831-45e4-9eb0-304617abca97.jpg&w=120&h=177&zc=1""
> alt="Poster" /></a>
> <h3>Today's Showtimes</h3>
> <ul class="showtimes">
> <li><a href="#" class="check-availability" rel="666629">9:35pm</a></li> </ul> </div>
> <div class="second-col">
> <ul class="meta">
> <li><b>Rating:</b> PG13</li><li><b>Run Time:</b> 122 minutes</li><li><b>Director:</b> John Crowley</li><li><b>Format:</b>
> DCP</li><li><b>Language:</b> English</li><li><b>Age Policy:</b> 13 and
> Up with Guardian</li><li><b>Note:</b> OPENS AT NITEHAWK ON FRIDAY,
> JANUARY 22</li> </ul>
> <div class="content">
> <p>BROOKLYN tells the profoundly moving story of Eilis Lacey (Saoirse Ronan), a young Irish immigrant navigating her way
> through 1950s Brooklyn. Lured by the promise of America, Eilis departs
> Ireland and the comfort of her mother’s home for the shores of New
> York City. The … <a
> href="http://www.nitehawkcinema.com/movie/brooklyn/">More Info</a></p>
> </div> </div>
> <div class="clear"> </div>
>
> </li>
> <div class="clear clear-divide"> </div>
>
> <li id="post-666679" class="post-666679 movie type-movie status-publish has-post-thumbnail hentry tag-alicia-vikander
> tag-amber-heard tag-ben-whishaw tag-eddie-redmayne tag-the-danish-girl
> tag-tom-hooper" >
> <h2><a href="http://www.nitehawkcinema.com/movie/the-danish-girl/"
> title="Permalink to THE DANISH GIRL" rel="bookmark">THE DANISH
> GIRL</a></h2>
> <div class="first-col">
> <a href="http://www.nitehawkcinema.com/movie/the-danish-girl/"
> class="poster-image-small"><img
> src="/timthumb.php?src=/wp-content/uploads/2016/01/MV5BMjA0NjA4NjE2Nl5BMl5BanBnXkFtZTgwNzIxNTY2NjE@._V1_SX214_AL_.jpg&w=120&h=177&zc=1""
> alt="Poster" /></a>
> <h3>Today's Showtimes</h3>
> <ul class="showtimes">
> <li><a href="#" class="check-availability" rel="666687">9:50pm</a></li> </ul> </div>
> <div class="second-col">
> <ul class="meta">
> <li><b>Rating:</b> R</li><li><b>Run Time:</b> 120 minutes</li><li><b>Director:</b> Tom Hooper</li><li><b>Format:</b>
> DCP</li><li><b>Language:</b> English</li><li><b>Age Policy:</b> 18 and
> Up</li> </ul>
> <div class="content">
> <p>With support from his loving wife Gerda, artist Einar Wegener prepares to undergo one of the first sex-change
> operations. The remarkable love story inspired by the lives of artists
> Lili Elbe and Gerda Wegener (portrayed by Academy Award winner Eddie
> Redmayne (The Theory of Everything) … <a
> href="http://www.nitehawkcinema.com/movie/the-danish-girl/">More Info</a></p>
> </div> </div>
> <div class="clear"> </div>