尝试从String中提取年份,但这似乎不起作用:
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"加上额外的"?"。我出错的任何想法?
答案 0 :(得分:3)
代码运行良好,你没有看到所有匹配的原因是因为findAll返回一个迭代器,如果你将它转换为Seq它将选择一个惰性集合的Stream集合(它将执行匹配为你要求元素)
要查看所有结果,请执行此操作
println(p.findAllIn(str).toList)
这会将迭代器转换为List(此操作将提取所有匹配的年份)。
那是什么意思?是否尚未计算流的下一个元素