2017-01-17 62 views
2

試圖從字符串中提取年,但這似乎並沒有工作:斯卡拉正則表達式:如何從字符串中提取年份?

val p = "(19|20)\\d\\d".r 
val str = "At the time of its release, Twilight Princess was considered the greatest entry in the Zelda series by many critics, including writers for 1UP.com, Computer and Video Games, Electronic Gaming Monthly, Game Informer, GamesRadar, IGN, and The Washington Post. It received several Game of the Year awards, and was the most critically acclaimed game of 2006. In 2011, the Wii version was rereleased under the Nintendo Selects label. A high-definition port for the Wii U, The Legend of Zelda: Twilight Princess HD, will be released in March 2016." 
println(p.findAllIn(str).toSeq) 

它給了我:

Seq[String] = Stream(2006, ?) 

缺少了「2016」和「2011」加上一個額外的「? 」。任何想法,我要去錯了嗎?

回答

3

代碼運行良好,你沒有看到所有匹配的原因是因爲findAll返回一個迭代器,如果你將它轉換爲一個Seq,它會選擇一個延遲集合的Stream集合(它將執行匹配如你要求的元素)

要查看所有結果做到這一點

println(p.findAllIn(str).toList) 

這將迭代器轉換到一個列表(此操作將提取所有匹配年)。

那是什麼意思?是流的下一個元素還沒有被計算出來

相關問題