如何正确使用scala.util.matching.Regex?(How to use scala.util.matching.Regex correctly?)

编程入门 行业动态 更新时间:2024-10-23 11:26:10
如何正确使用scala.util.matching.Regex?(How to use scala.util.matching.Regex correctly?)

这可能看起来很明显,但我无法解释No match available error 。 下面,您可以找到我正在使用的简单匹配函数的定义。

函数内部的相同指令运行没有问题,但是调用函数会引发错误。 你能帮我找出错误吗?

import scala.util.matching.Regex def regexParsing(inputRecord:String, inputRegex:String, listOfFields:Seq[String], fieldsToRemove:Seq[String]): scala.collection.Map[String,Any] = { val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) return resultMap } val inputRecord = """s2222f""" val inputRegex = """(.*)""" val listOfFields = Seq("field") val fieldsToRemove = Seq("field1", "field2") // working val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) // not working regexParsing(inputRecord, inputRegex, listOfFields, fieldsToRemove)

This may look obvious but I couldn't explain the No match available error . Below, you find a definition of a simple matching function I am using.

The same instructions inside the function run without an issue, however calling the function raises the error. Can you help me pinpoint the mistake ?

import scala.util.matching.Regex def regexParsing(inputRecord:String, inputRegex:String, listOfFields:Seq[String], fieldsToRemove:Seq[String]): scala.collection.Map[String,Any] = { val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) return resultMap } val inputRecord = """s2222f""" val inputRegex = """(.*)""" val listOfFields = Seq("field") val fieldsToRemove = Seq("field1", "field2") // working val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) // not working regexParsing(inputRecord, inputRegex, listOfFields, fieldsToRemove)

最满意答案

试试2.12? 推进迭代器的限制是最终解决的API中的问题。

$ scala Welcome to Scala 2.12.0-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101). Type in expressions for evaluation. Or try :help. scala> :pa // Entering paste mode (ctrl-D to finish) import scala.util.matching.Regex def regexParsing(inputRecord:String, inputRegex:String, listOfFields:Seq[String], fieldsToRemove:Seq[String]): scala.collection.Map[String,Any] = { val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) return resultMap } val inputRecord = """s2222f""" val inputRegex = """(.*)""" val listOfFields = Seq("field") val fieldsToRemove = Seq("field1", "field2") // working val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) // Exiting paste mode, now interpreting. import scala.util.matching.Regex regexParsing: (inputRecord: String, inputRegex: String, listOfFields: Seq[String], fieldsToRemove: Seq[String])scala.collection.Map[String,Any] inputRecord: String = s2222f inputRegex: String = (.*) listOfFields: Seq[String] = List(field) fieldsToRemove: Seq[String] = List(field1, field2) logPattern: scala.util.matching.Regex = (.*) result: scala.util.matching.Regex.MatchIterator = non-empty iterator resultMap: scala.collection.immutable.Map[String,String] = Map(field -> s2222f) scala> regexParsing(inputRecord, inputRegex, listOfFields, fieldsToRemove) res0: scala.collection.Map[String,Any] = Map(field -> s2222f) scala> :quit

Try 2.12? The restriction about advancing the iterator is a gotcha in the API that was finally addressed.

$ scala Welcome to Scala 2.12.0-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101). Type in expressions for evaluation. Or try :help. scala> :pa // Entering paste mode (ctrl-D to finish) import scala.util.matching.Regex def regexParsing(inputRecord:String, inputRegex:String, listOfFields:Seq[String], fieldsToRemove:Seq[String]): scala.collection.Map[String,Any] = { val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) return resultMap } val inputRecord = """s2222f""" val inputRegex = """(.*)""" val listOfFields = Seq("field") val fieldsToRemove = Seq("field1", "field2") // working val logPattern = new Regex(inputRegex, listOfFields:_*) val result = logPattern.findAllIn(inputRecord) val resultMap = result.groupNames.map(a => Map(a.toString -> result.group(a))).reduce(_++_) // Exiting paste mode, now interpreting. import scala.util.matching.Regex regexParsing: (inputRecord: String, inputRegex: String, listOfFields: Seq[String], fieldsToRemove: Seq[String])scala.collection.Map[String,Any] inputRecord: String = s2222f inputRegex: String = (.*) listOfFields: Seq[String] = List(field) fieldsToRemove: Seq[String] = List(field1, field2) logPattern: scala.util.matching.Regex = (.*) result: scala.util.matching.Regex.MatchIterator = non-empty iterator resultMap: scala.collection.immutable.Map[String,String] = Map(field -> s2222f) scala> regexParsing(inputRecord, inputRegex, listOfFields, fieldsToRemove) res0: scala.collection.Map[String,Any] = Map(field -> s2222f) scala> :quit

更多推荐

本文发布于:2023-08-06 16:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454601.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何正确   scala   util   correctly   Regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!