Regex Scala中第n个模式匹配的索引
我有一个程序,试图找到字母“e”第n次出现的索引。我猜是这样的Regex Scala中第n个模式匹配的索引,regex,scala,Regex,Scala,我有一个程序,试图找到字母“e”第n次出现的索引。我猜是这样的 def findE(line: String, ignore: Int) : Int = { val pattern = "e".r val index = line.indexOf(pattern(ignore+1)) index } 在哪里 是所需的组,但语法无效。想知道是否有人知道怎么做吗?如果我是你,我会使用标准的组合器 > "abcdeabcdeabcde".zipWithIndex.col
def findE(line: String, ignore: Int) : Int = {
val pattern = "e".r
val index = line.indexOf(pattern(ignore+1))
index
}
在哪里
是所需的组,但语法无效。想知道是否有人知道怎么做吗?如果我是你,我会使用标准的组合器
> "abcdeabcdeabcde".zipWithIndex.collect {
case ('e', index) => index
}
res1: collection.immutable.IndexedSeq[Int] = Vector(4, 9, 14)
只要取索引5中的任何东西,如果它存在的话,这就是你的答案。如果我是你,我会使用标准的组合词
> "abcdeabcdeabcde".zipWithIndex.collect {
case ('e', index) => index
}
res1: collection.immutable.IndexedSeq[Int] = Vector(4, 9, 14)
只要取索引5中的任何东西,如果它存在的话,这就是你的答案