I'm currently working my way through a Scala project, and converting it into Java. Everythings going fine, but I'm stumbled into this snippet:
Pattern fileNamePattern = Pattern.compile("^(\\w+).*(_\\w+\\.xml)$");
new File(filePath).getName match {
case FileNamePattern(first, last) => return first + last
case n => return n
}
I understand the regex, One or more letters, numbers or punctuation, followed by 0 or more characters, followed by One or more letters, numbers or punctuation. The purpose of this function is to get the file name from a file path, but that is really very straight forward in Java, so I would have thought the Scala developer wouldn't make it so needlessly complex.
The problem is, I don't want to march ahead and assume the developer was an idiot, when maybe they're trying to do something a little more clever, and my lack of experience with Scala is stopping me from seeing it. So could someone please explain:
- The syntax with match
- Where the hell first and last came from
- The equivalent / documentation that leads too the Java equivalent of this snippet
def getFileName(filePath: String): String = {
if(filePath == null || filePath.trim.length == 0) {
return filePath
}
val FileNamePattern = new Regex("^(\\w+).*(_\\w+\\.xml)$")
new File(filePath).getName match {
case FileNamePattern(first, last) => return first + last
case n => return n
}
}
\wis most certainly not whitespace.