4

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
  • 1
    I get the same failure in both code blocks (in or out of method) - very odd. Looks like some kind of issue with accessing result.group(..) before the match iterator is materialized somehow - if you add something silly like println(result) before creating resultMap this works (!). Commented Oct 17, 2016 at 17:12
  • 1
    @TzachZohar yes, there's an old mailing list thread where someone noticed it working in REPL because REPL invokes toString. Commented Oct 17, 2016 at 18:26

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.