如何在StandardTokenParsers中定义正则表达式以标识路径? [英] How to define a Regex in StandardTokenParsers to identify path?
问题描述
我正在编写一个解析器,我想在其中解析像这样的算术表达式: /hdfs://xxx.xx.xx.x:xxxx/path1/file1.jpg+1 我想解析它,将后缀更改为后缀并进行计算.我在其他讨论中的代码的一部分中也使用了帮助.
I am writing a parser in which I want to parse arithmetic expressions like: /hdfs://xxx.xx.xx.x:xxxx/path1/file1.jpg+1 I want to parse it change the infix to postfix and do the calculation. I used helps from a part of code in another discussion as well.
class InfixToPostfix extends StandardTokenParsers {
import lexical._
def regexStringLit(r: Regex): Parser[String] = acceptMatch(
"string literal matching regex " + r,
{ case StringLit(s) if r.unapplySeq(s).isDefined => s })
def pathIdent: Parser[String] =regexStringLit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)
lexical.delimiters ++= List("+","-","*","/", "^","(",")",",")
def value :Parser[Expr] = numericLit ^^ { s => Number(s) }
def variable:Parser[Expr] = pathIdent ^^ { s => Variable(s) }
def parens:Parser[Expr] = "(" ~> expr <~ ")"
def argument:Parser[Expr] = expr <~ (","?)
def func:Parser[Expr] = ( pathIdent ~ "(" ~ (argument+) ~ ")" ^^ { case f ~ _ ~ e ~ _ => Function(f, e) })
def term = (value | parens | func | variable)
// Needed to define recursive because ^ is right-associative
def pow :Parser[Expr] = ( term ~ "^" ~ pow ^^ {case left ~ _ ~ right => BinaryOperator(left, "^", right) }|
term)
def factor = pow * ("*" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "*", right) } |
"/" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "/", right) } )
def sum = factor * ("+" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "+", right) } |
"-" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "-", right) } )
def expr = ( sum | term )
def parse(s:String) = {
val tokens = new lexical.Scanner(s)
phrase(expr)(tokens)
}
//和其余的代码
借助此答案,我能够解决以下错误:
I was able to solve the following errors with the help of this answer:
ScalaParser.scala:192: invalid escape character
[error] def pathIdent: Parser[String] =regexStringLit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)
[error] ^
[error] ScalaParser.scala:192: invalid escape character
[error] def pathIdent: Parser[String] =regexStringLit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)
[error] ^
[error] ScalaParser.scala:192: invalid escape character
[error] def pathIdent: Parser[String] =regexStringLit("/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+))".r)
[error] ^
随着pathIdent的更改:
With the change of pathIdent to this:
def pathIdent: Parser[String] =regexStringLit("/hdfs://([\\d.]+):(\\d+)/([\\w/]+/(\\w+\\.w+))".r)
现在我遇到了运行时错误,提示:
Now I am getting a run time error which says:
[1.1] failure: string literal matching regex /hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+)) expected
/hdfs://111.33.55.2:8888/folder1/p.a3d+1
^
它正在使用JavaTokenParsers进行工作,但是有当前更改,因此我不得不使用StandardTokenParsers.
It was working using JavaTokenParsers but with current changes and I had to use StandardTokenParsers.
推荐答案
在双引号字符串中,反斜杠是转义字符.如果要在双引号字符串中使用文字反斜杠,则必须对其进行转义,因此"\d"
应该为"\\d"
.
In a double quoted string backslash is an escape character. If you mean to use the literal backslash in a double quotes string you must escape it, thus "\d"
should be "\\d"
.
此外,由于字符类中的点没有特殊含义,因此您无需在字符类中对正则表达式点进行转义.因此"[\d.]"
应该只是"[\ d.]".
Furthermore you do not need to escape the regex dot within a character class, since dot has no special meaning with a character class. So "[\d.]"
should just be "[\d.]".
您还可以通过使用原始插值器或使用三引号的多行字符串文字来放弃所有这些转义业务.
You can also forgo all this escaping business by using the raw interpolator or multi-line string literals using triple quotes.
这篇关于如何在StandardTokenParsers中定义正则表达式以标识路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!