【发布时间】:2018-02-26 21:57:02
【问题描述】:
我希望我的正则表达式打印 { 或 {{ 之前的所有内容(不包括它们。
到目前为止我所拥有的是:
class ExpressionParser extends RegexParsers {
val regExpr = """^.*?((?=\{{2})|(?=\{)|$)""".r //not sure about the "$". Added it because test case 1 wasn't printing. see below
def program: Parser[Any] = regExpr
}
这是我的测试:
object Test {
def main(args: Array[String]): Unit = {
val p = new ExpressionParser()
val test = p.parseAll(p.program, 'tests go here') // doesn't print anything
if(test.successful) println(test.get)
// 用其中的每一个替换 'tests go here'
//"This is plain text so should always print") // this isn't printing so make checks for { optional
//"abc {{"
//"abc de{ fg{{{ hi"
//"abc } {{ {{ de{' fg{{{ hi")
}
}
我希望它打印出来:
//This is plain text so should always print
//abc
//abc de
//abc {
仅打印第一个测试。为什么?
谢谢!
【问题讨论】:
-
我用班级的内容编辑了帖子
-
请提供minimal reproducible example。不清楚你编码了什么,在哪里声明/定义了什么。
-
我编辑了整个内容。现在更清楚了
标签: regex scala pattern-matching lookahead negative-lookahead