Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Scala锚定正则表达式充当未编排的正则表达式_Regex_Scala - Fatal编程技术网

Regex Scala锚定正则表达式充当未编排的正则表达式

Regex Scala锚定正则表达式充当未编排的正则表达式,regex,scala,Regex,Scala,因此,出于某种原因,在Scala2.11中,我的锚定正则表达式模式充当未锚定正则表达式模式 scala> """something\.com""".r.anchored findFirstIn "app.something.com" res66: Option[String] = Some(something.com) scala> """^.something\.com$""".r.anchored findFirstIn "app.something.com" res65: Op

因此,出于某种原因,在Scala2.11中,我的锚定正则表达式模式充当未锚定正则表达式模式

scala> """something\.com""".r.anchored findFirstIn "app.something.com"
res66: Option[String] = Some(something.com)
scala> """^.something\.com$""".r.anchored findFirstIn "app.something.com"
res65: Option[String] = None
我原以为第一个表达式的计算结果与第二个表达式(手动输入的锚点)一样为
None
,但它不是

任何帮助都将不胜感激。

该方法自动取消正则表达式的锚定

您可以看到示例代码也仅与
A
匹配:

示例:
“\w+”.r findFirstIn“一个简单的示例。”foreach println//打印“A”

顺便说一句,一旦您创建了一个类似于
“pattern”.r的正则表达式,它在默认情况下被锚定,但这仅在
匹配
块中使用正则表达式时才起作用。在
FindAllIn
FindFirstIn
中,这种类型的锚定被忽略


因此,为了确保正则表达式与整个字符串匹配,如果您不确定要在哪里使用正则表达式,请始终添加
^
$
(或
\A
\z
)锚定。

我认为,它只应该与匹配一起工作:

val reg = "o".r.anchored
"foo" match {
   case reg() => "Yes!"
   case _ => "No!"
 }
。。。返回“否!”。 这似乎不是很有用,因为默认情况下只锚定了
“o”.r
。我能想象的唯一用途是,如果你做了一些未编排的
(偶然?:),然后想撤销它,或者如果你只是想匹配这两种情况,但是
分别地:

val reg = "o".r.unanchored
"foo" match {
   case reg.anchored() => "Anchored!
   case reg() => "Unanchored"
   case _ => "I dunno"
}