How to create a parser from Regex in Scala to parse a path?

后端 未结 2 2025
半阙折子戏
半阙折子戏 2021-01-25 04:43

I am writing a parser in which I am trying to parse a path and do arithmetic calculations. since I cannot use RegexParsers with StandardTokenParsers I am trying to make my own.

相关标签:
2条回答
  • 2021-01-25 05:04

    There are a couple of mistakes in you regex:

    /hdfs://([\d.]+):(\d+)/([\w/]+/(\w+\.w+))

    1) There are unnecessary parenthesis (or your forgot a +) - this is not a real mistake but makes it harder to read your regex and fix bugs.

    /hdfs://[\d.]+:\d+/[\w/]+/\w+\.w+

    2) The last w+ is not escaped:

    /hdfs://[\d.]+:\d+/[\w/]+/\w+\.\w+

    3) You only allow . but not + for the last part:

    /hdfs://[\d.]+:\d+/[\w/]+/\w+([.+]\w+)+

    The above expression matches your test case, however, I do suspect, you actually want this expression:

    /hdfs://\d+(\.\d+){3}:\d+(/(\w+([-+.*/]\w+)*))+

    0 讨论(0)
  • 2021-01-25 05:05

    I solved it writing a trait and using JavaTokenParsers rather than StandardToken Parser.

     trait pathIdentifier extends RegexParsers{
    
          def pathIdent: Parser[String] ={
              """hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))""".r
        }
    }
    

    @Tilo Thanks for your help your solution is working as well but changing extended class to JavaTokenParser helped to solve the problem.

    0 讨论(0)
提交回复
热议问题