在模式匹配时,是否可以使非捕获组在scala正则表达式中工作

时间:2021-10-13 12:14:42

As far as I can see from the docs, non-capturing groups are defined by (:? ), as in Java. (I believe it's the same underlying library).

据我所知,非文档中的非捕获组由(:?)定义,如Java中所示。 (我相信它是相同的底层库)。

However, this doesn't seem to work:

但是,这似乎不起作用:

var R = "a(:?b)c".r
R.findFirstMatchIn("abc").get.group(1)

returns "b" (when it should be empty). I suspect this is not normally a problem, but when doing pattern matching, it means that I can't now do:

返回“b”(当它应为空时)。我怀疑这通常不是问题,但在进行模式匹配时,这意味着我现在无法做到:

"abc" match {case R => println("ok");case _ => println("not ok")}
> not ok

I have to do:

我要做:

"abc" match {case R(x) => println("ok");case _ => println("not ok")}
> ok

Is there any way to make this work "as expected"?

有没有办法让这项工作“按预期”进行?

2 个解决方案

#1


7  

In addition to the correct answer, use val and parens:

除了正确的答案,请使用val和parens:

scala> val R = "a(?:b)c".r  // use val
R: scala.util.matching.Regex = a(?:b)c

scala> "abc" match {case R() => println("ok");case _ => println("not ok")} // parens not optional
ok

You can also always use the wildcard sequence and not care whether you specified capturing groups. I discovered this recently and find it is most clear and robust.

您也可以始终使用通配符序列,而不关心是否指定了捕获组。我最近发现了这一点,发现它最清晰,最强大。

scala> "abc" match {case R(_*) => println("ok");case _ => println("not ok")} 
ok

If anything matches, _* will, including an extractor returning Some(null).

如果匹配,_ *将,包括返回Some(null)的提取器。

#2


3  

It seems like you've got the syntax wrong. Should be (?:).

看起来你的语法错了。应该 (?:)。

http://docs.oracle.com/javase/tutorial/essential/regex/groups.html

http://docs.oracle.com/javase/tutorial/essential/regex/groups.html

Groups beginning with (? are pure, non-capturing groups that do not capture text and do not count towards the group total.

以(?开头的组是纯粹的非捕获组,不捕获文本而不计入组总数。

#1


7  

In addition to the correct answer, use val and parens:

除了正确的答案,请使用val和parens:

scala> val R = "a(?:b)c".r  // use val
R: scala.util.matching.Regex = a(?:b)c

scala> "abc" match {case R() => println("ok");case _ => println("not ok")} // parens not optional
ok

You can also always use the wildcard sequence and not care whether you specified capturing groups. I discovered this recently and find it is most clear and robust.

您也可以始终使用通配符序列,而不关心是否指定了捕获组。我最近发现了这一点,发现它最清晰,最强大。

scala> "abc" match {case R(_*) => println("ok");case _ => println("not ok")} 
ok

If anything matches, _* will, including an extractor returning Some(null).

如果匹配,_ *将,包括返回Some(null)的提取器。

#2


3  

It seems like you've got the syntax wrong. Should be (?:).

看起来你的语法错了。应该 (?:)。

http://docs.oracle.com/javase/tutorial/essential/regex/groups.html

http://docs.oracle.com/javase/tutorial/essential/regex/groups.html

Groups beginning with (? are pure, non-capturing groups that do not capture text and do not count towards the group total.

以(?开头的组是纯粹的非捕获组,不捕获文本而不计入组总数。