Mahsa
Mahsa

Reputation: 1550

How to write a Scala parser for paths?

I am a beginner in scala writing a scala parser in which I need to parse a path like this (suppose x is an integer): /hfds://xxx.xxx.xx.xxx:xxxx/path1/path2/filename1.jpg

I tried the following not sure if I am on the right track:

  def pathIdent: Parser[String] = ("/hdfs""+"""(\d+~\.\d*)"""+ ~("""(u[a-zA-Z0-9])*+"""+"\.[\\'"jpg]"").r

I know it has errors! I really need help on this!

Upvotes: 0

Views: 547

Answers (1)

fredfred
fredfred

Reputation: 341

You regex looks like it has typos. Try with something like this:

scala> val regex = """^/hfds://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+)$)""".r
regex: scala.util.matching.Regex = ^/hfds://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+)$)

Some sample path:

scala> val path = """/hfds://111.222.33.444:5555/path1/path2/filename1.jpg"""
path: String = /hfds://111.222.33.444:5555/path1/path2/filename1.jpg

Then do the matching:

scala> val regex(numbers,afterColon,fullPath,filename) = path
numbers: String = 111.222.33.444
afterColon: String = 5555
fullPath: String = path1/path2/filename1.jpg
filename: String = filename1.jpg

To catch parsing errors you can use pattern matching like this, e.g. parsing the filename:

def parseFilename(path: String): Option[String] = path match {
     case regex(numbers,afterColon,fullPath,filename) => Some(filename)
     case _ => println("parse error");None
}

It will return a Option of that filename:

scala> parseFilename(path)
res0: Option[String] = Some(filename1.jpg)

scala> val badPath="""/hfds://xxx.xxx.xx.xxx:xxxx/path1/path2/filename1.jpg"""
badPath: String = /hfds://xxx.xxx.xx.xxx:xxxx/path1/path2/filename1.jpg

scala> parseFilename(badPath)
parse error
res1: Option[String] = None

Upvotes: 3

Related Questions