如何正确使用scala.util.matching.Regex?
发布时间:2020-12-16 10:01:08 所属栏目:安全 来源:网络整理
导读:这可能看起来很明显,但我无法解释无匹配可用错误.下面,您可以找到我正在使用的简单匹配函数的定义. 函数内部的相同指令运行没有问题,但是调用函数会引发错误.你能帮我找出错误吗? import scala.util.matching.Regexdef regexParsing(inputRecord:String,inp
这可能看起来很明显,但我无法解释无匹配可用错误.下面,您可以找到我正在使用的简单匹配函数的定义.
函数内部的相同指令运行没有问题,但是调用函数会引发错误.你能帮我找出错误吗? 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,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,fieldsToRemove) res0: scala.collection.Map[String,Any] = Map(field -> s2222f) scala> :quit (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- HDU 5239 Doom [线段树,更新有上界]【数据结构】
- scala – Class X采用类型参数
- 在使用angularjs过程,ng-repeat中track by的作用
- webpack angular2-rc5 uri.match不是一个函数
- Shell 脚本语法
- Bootstrap3基础 text-right/left/center 设置标题
- angularjs 表单点击保存,获取修改的信息项,进行提
- angularjs指令中的replace与transclude参数
- angularjs – Angular的$watch的奇怪行为
- What is the diference between putting a prope
热点阅读