为什么在Scala中,迭代器[Match]给出了长度,但没有数据?

为什么在Scala中,迭代器[Match]给出了长度,但没有数据?,scala,Scala,我试图使用regex.findAllMatchIn和迭代器[match]匹配下面的大括号文本。下面的代码显示MatcheOne在某些情况下的长度为非零,但随后表示它是一个空迭代器。我觉得我错过了一些基本的东西。有什么想法吗 import scala.util.matching.Regex.Match import scala.xml._ val xmldata = <document> <content> <headers>

我试图使用regex.findAllMatchIn和迭代器[match]匹配下面的大括号文本。下面的代码显示MatcheOne在某些情况下的长度为非零,但随后表示它是一个空迭代器。我觉得我错过了一些基本的东西。有什么想法吗

  import scala.util.matching.Regex.Match
  import scala.xml._

  val xmldata = <document>
    <content>
      <headers>
      </headers>
      <body>
        Foo [1], then another foo[2]; then lots of other things here
        And add a few other lines[2][3] of test data[3][5] (Foo 1234)
      </body>
    </content>
   </document>

  val bodyIterator : Iterator[String]= ((xmldata \ "content" \ "body").text).linesWithSeparators

  while (bodyIterator.hasNext) {
    val line = bodyIterator.next()

    println(s"*****   Line is: $line")

    val citationOne = """(\[[0-9]+\])(,\[[0-9]+\])*""".r
    val citationTwo = """(\([A-Z, -.]+[0-9]{4}\))""".r
    /* search the line for citations */

    val matchesOne: Iterator[Match] = citationOne.findAllMatchIn(line)
    val matchesTwo: Iterator[Match] = citationTwo.findAllMatchIn(line)

    println("matchesOne found: " + matchesOne.length)
    println("matchesTwo found: " + matchesTwo.length)
    for (m <- matchesOne) {println(s"match is $m")}

    println("matchesOne Matches: ")
    matchesOne.foreach(x => println("1: " + x.matched))
    //while (matchesOne.hasNext) {
    // println("matchesOne: " + matchesOne.next())
    // }

    while (matchesTwo.hasNext) {
      println("matchesTwo: " + matchesTwo.next().matched)
    }

    println("\n\n")
  }
导入scala.util.matching.Regex.Match
导入scala.xml_
val xmldata=
Foo[1],然后是另一个Foo[2];还有很多其他的事情
并添加其他几行[2][3]测试数据[3][5](Foo 1234)
val bodyIterator:Iterator[String]=((xmldata\“content”\“body”).text).lineswithseparator
while(bodyIterator.hasNext){
val line=bodyIterator.next()
println(s“****行为:$Line”)
val-引文one=“”(\[[0-9]+\])(,\[[0-9]+\])*“”
val引文2=“”(\([A-Z,-.]+[0-9]{4}\)”。.r
/*在这行中搜索引文*/
val matchesOne:Iterator[Match]=引文一。findAllMatchIn(行)
val matchesTwo:Iterator[Match]=引文2.findAllMatchIn(行)
println(“找到匹配项:“+matchesOne.length”)
println(“找到匹配两个:“+matchesTwo.length”)
对于(m println(“1:+x.matched))
//while(matchesOne.hasNext){
//println(“matchesOne:+matchesOne.next())
// }
while(匹配两个hasNext){
println(“matchesTwo:+matchesTwo.next().matched)
}
println(“\n\n”)
}
输出:

  import scala.util.matching.Regex.Match
  import scala.xml._

  xmldata: scala.xml.Elem = <document>
    <content>
      <headers>
      </headers>
      <body>
        Foo [1], then another foo[2]; then lots of other things here
        And add a few other lines[2][3] of test data[3][5] (Foo 1234)
      </body>
      </content>
     </document>

  bodyIterator: Iterator[String] = non-empty iterator

  *****   Line is: 

  matchesOne found: 0
  matchesTwo found: 0
  matchesOne Matches: 



  *****   Line is:       Foo [1], then another foo[2]; then lots of other things here

  matchesOne found: 2
  matchesTwo found: 0
  matchesOne Matches: 



  *****   Line is:       And add a few other lines[2][3] of test data[3][5] (Foo 1234)

  matchesOne found: 4
  matchesTwo found: 0
  matchesOne Matches: 



  *****   Line is:     
  matchesOne found: 0
  matchesTwo found: 0 
导入scala.util.matching.Regex.Match
导入scala.xml_
xmldata:scala.xml.Elem=
Foo[1],然后是另一个Foo[2];还有很多其他的事情
并添加其他几行[2][3]测试数据[3][5](Foo 1234)
Body迭代器:迭代器[字符串]=非空迭代器
*****行是:
找到匹配项:0
找到两个匹配项:0
MatcheOne匹配项:
*****行是:Foo[1],然后是另一个Foo[2];还有很多其他的事情
matchesOne发现:2
找到两个匹配项:0
MatcheOne匹配项:
*****行为:并添加测试数据[3][5]的其他几行[2][3](Foo 1234)
matchesOne发现:4
找到两个匹配项:0
MatcheOne匹配项:
*****行是:
找到匹配项:0
找到两个匹配项:0
调用会排出,如中所述:

注意-重用:调用此方法后,应该放弃调用它的迭代器


计算迭代器的长度会消耗它(因为它必须处理所有元素才能看到它的长度)。因此,在已知长度后,迭代器现在为空
!

当您获得迭代器的长度时,您已经在迭代器的末尾,因此之后无法获得任何数据。在您的情况下,解决方案是将其转换为类似列表的内容

   val matchesOne: List[Match] = citationOne.findAllMatchIn(line).toList
   val matchesTwo: List[Match] = citationTwo.findAllMatchIn(line).toList
然后您将获得预期的输出,例如:

scala> val line = "Foo [1], then another foo[2]; then lots of other things here"
line: String = Foo [1], then another foo[2]; then lots of other things here

scala> val result = citationOne.findAllMatchIn(line).toList
result: List[scala.util.matching.Regex.Match] = List([1], [2])

scala> val matchesOne = citationOne.findAllMatchIn(line).toList
matchesOne: List[scala.util.matching.Regex.Match] = List([1], [2])

scala> println("matchesOne found: " + matchesOne.length)
matchesOne found: 2

scala> for (m <- matchesOne) {println(s"match is $m")}
match is [1]
match is [2]
scala>val line=“Foo[1],然后是另一个Foo[2];然后这里还有很多其他东西”
行:String=Foo[1],然后是另一个Foo[2];还有很多其他的事情
scala>val结果=引文1.findAllMatchIn(行).toList
结果:List[scala.util.matching.Regex.Match]=List([1],[2])
scala>val matchesOne=引文一.findAllMatchIn(行).toList
matcheOne:List[scala.util.matching.Regex.Match]=List([1],[2])
scala>println(“找到匹配项:“+matchesOne.length”)
matchesOne发现:2

scala>for(啊,错过了文档中的这一部分。如果有迭代器,这完全有道理。谢谢!!现在一切就绪。谢谢大家!!