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

后端 未结 2 2026
半阙折子戏
半阙折子戏 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条回答
  •  梦毁少年i
    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+)*))+

提交回复
热议问题