访问 Scala 解析器正则表达式匹配数据
我想知道是否可以从下面的语法中的匹配正则表达式生成MatchData。
object DateParser extends JavaTokenParsers {
....
val dateLiteral = """(\d{4}[-/])?(\d\d[-/])?(\d\d)""".r ^^ {
... get MatchData
}
}
当然,一种选择是在块内再次执行匹配,但由于 RegexParser 已经执行了匹配,我希望它将 MatchData 传递到块或存储它?
I wondering if it's possible to get the MatchData generated from the matching regular expression in the grammar below.
object DateParser extends JavaTokenParsers {
....
val dateLiteral = """(\d{4}[-/])?(\d\d[-/])?(\d\d)""".r ^^ {
... get MatchData
}
}
One option of course is to perform the match again inside the block, but since the RegexParser has already performed the match I'm hoping that it passes the MatchData to the block, or stores it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是将您的
Regex
转换为Parser
的隐式定义:只需调整它即可:
示例:
Here is the implicit definition that converts your
Regex
into aParser
:Just adapt it:
Example:
不,你不能这样做。如果您查看将正则表达式转换为解析器时使用的解析器的定义,它会丢弃所有上下文并仅返回完全匹配的字符串:
http://lampsvn.epfl.ch/trac/ scala/browser/scala/tags/R_2_7_7_final/src/library/scala/util/parsing/combinator/RegexParsers.scala?view=markup#L55
不过,您还有其他几个选择:
第一个看起来像
<~
意味着“将这两个标记放在一起,但只给出第一个标记的结果~
意味着“将这两个标记放在一起,并将它们绑定在一个可模式匹配的 ~ 对象中。?
表示解析器是可选的并将返回一个选项。.getOrElse
位在解析器未定义值时提供默认值。No, you can't do this. If you look at the definition of the Parser used when you convert a regex to a Parser, it throws away all context and just returns the full matched string:
http://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_7_7_final/src/library/scala/util/parsing/combinator/RegexParsers.scala?view=markup#L55
You have a couple of other options, though:
The first would look like
The
<~
means "require these two tokens together, but only give me the result of the first one.The
~
means "require these two tokens together and tie them together in a pattern-matchable ~ object.The
?
means that the parser is optional and will return an Option.The
.getOrElse
bit provides a default value for when the parser didn't define a value.当在 RegexParsers 实例中使用 Regex 时,RegexParsers 中的隐式 def regex(Regex): Parser[String] 用于将该 Regex 应用于输入。在当前输入成功应用 RE 时生成的 Match 实例用于在 regex() 方法中构造 Success,但仅使用其“结束”值,因此在该方法时任何捕获的子匹配都会被丢弃返回。
就目前情况而言(在我查看的 2.7 源代码中),我相信你运气不好。
When a Regex is used in a RegexParsers instance, the implicit def regex(Regex): Parser[String] in RegexParsers is used to appoly that Regex to the input. The Match instance yielded upon successful application of the RE at the current input is used to construct a Success in the regex() method, but only its "end" value is used, so any captured sub-matches are discarded by the time that method returns.
As it stands (in the 2.7 source I looked at), you're out of luck, I believe.
我使用 scala 2.8.1 遇到了类似的问题,并尝试使用 RegexParsers 类解析“name:value”形式的输入:
似乎您可以在解析后获取匹配的组,如下所示:
I ran into a similar issue using scala 2.8.1 and trying to parse input of the form "name:value" using the
RegexParsers
class:It seems that you can grab the matched groups after parsing like this: