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.
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):
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…