Scala 正则表达式命名捕获组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3029657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala regex Named Capturing Groups
提问by oluies
In scala.util.matching.Regex trait MatchDataI see that there support for groupnames , I thought that this was related to (Regex Named Capturing Groups)
在scala.util.matching.Regex trait MatchData 中,我看到对 groupnames 的支持,我认为这与 ( Regex Named Capturing Groups) 有关
But since Java does not support groupnames until version 7as I understand it (ref), Scala version 2.8.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.gives me this exception:
但由于Java 直到版本 7 才支持组名(参考),Scala 版本 2.8.0(Java HotSpot(TM) 64 位服务器 VM,Java 1.6。给了我这个例外:
scala> val pattern = """(?<login>\w+) (?<id>\d+)""".r
java.util.regex.PatternSyntaxException: Look-behind group does not have an obvio
us maximum length near index 11
(?<login>\w+) (?<id>\d+)
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.group0(Pattern.java:2488)
at java.util.regex.Pattern.sequence(Pattern.java:1806)
at java.util.regex.Pattern.expr(Pattern.java:1752)
at java.util.regex.Pattern.compile(Pattern.java:1460)
So the question is Named Capturing Groups supported in Scala? If so any examples out there?
那么问题是 Scala 支持命名捕获组吗?如果是这样,有什么例子吗?
回答by polygenelubricants
I'm afraid that Scala's named groups aren't defined the same way. It's nothing but a post-processing alias to unnamed (i.e. just numbered) groups in the original pattern.
恐怕 Scala 的命名组的定义方式不同。它只不过是原始模式中未命名(即仅编号)组的后处理别名。
Here's an example:
下面是一个例子:
import scala.util.matching.Regex
object Main {
def main(args: Array[String]) {
val pattern = new Regex("""(\w*) (\w*)""", "firstName", "lastName");
val result = pattern.findFirstMatchIn("James Bond").get;
println(result.group("lastName") + ", " + result.group("firstName"));
}
}
This prints (as seen on ideone.com):
这打印(如在 ideone.com 上看到的):
Bond, James
What happens here is that in the constructor for the Regex, we provide the aliases for group 1, 2, etc. Then we can refer to these groups by those names. These names are not intrinsic in the patterns themselves.
这里发生的事情是,在 的构造函数中Regex,我们为组 1、2 等提供别名。然后我们可以通过这些名称来引用这些组。这些名称不是模式本身固有的。
回答by Randall Schulz
Scala does not have its own imlementation of regular expression matching. Instead the underlying regular expressions are Java's, so the details of writing patterns are those documented in java.util.regex.Pattern.
Scala 没有自己的正则表达式匹配实现。相反,底层的正则表达式是 Java 的,所以编写模式的细节是 java.util.regex.Pattern 中记录的那些。
There you will find that the syntax you're using is actually that of the look-behind constraint, though according to the docs the <must be followed by either =(positive look-behind) or !(negative look-behind).
在那里,您会发现您使用的语法实际上是后视约束的语法,但根据文档,<必须后跟=(positive look-behind) 或!(negative look-behind)。

