Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
如何在Scala的filterNot中使用正则表达式变量?_Scala - Fatal编程技术网

如何在Scala的filterNot中使用正则表达式变量?

如何在Scala的filterNot中使用正则表达式变量?,scala,Scala,使用Scala,我试图根据问题从数据中删除URL。下面的代码可以正常工作: val removeRegexUDF = udf( (input: Seq[String]) => input.filterNot(s => s.matches("(https?\\://)\\S+" )) filteredDF.withColumn("noURL", removeRegexUDF('filtered)).select("racist", "filtered","noURL").

使用Scala,我试图根据问题从数据中删除URL。下面的代码可以正常工作:

 val removeRegexUDF = udf(
    (input: Seq[String]) => input.filterNot(s => s.matches("(https?\\://)\\S+" ))

 filteredDF.withColumn("noURL", removeRegexUDF('filtered)).select("racist", "filtered","noURL").show(100, false)
现在我想使用一个变量而不是文字正则表达式,所以我尝试:

        val urls = """(https?\\://)\\S+"""
        val removeRegexUDF = udf(
        (input: Seq[String]) => input.filterNot(s => s.matches(urls ))
但这似乎对数据没有影响。我尝试:

val urls = """(https?\\://)\\S+""".r
但这会产生错误:

urls: scala.util.matching.Regex = (https?\\://)\\S+
<console>:45: error: type mismatch;
 found   : scala.util.matching.Regex
 required: String
         (input: Seq[String]) => input.filterNot(s => s.matches(urls) )
URL:scala.util.matching.Regex=(https?\\\:/)\\S+
:45:错误:类型不匹配;
找到:scala.util.matching.Regex
必需:字符串
(输入:Seq[String])=>input.filterNot(s=>s.matches(URL))

对于如何实现这一点的任何指导都非常感谢。

我想这与使用单引号和三引号有关。在第一个示例中,您添加了额外的反斜杠以转义字符,而在后一个示例中,您不需要这些字符-用三重引号包装字符串就足够了

println(“(https?\\:/)\\S+”/(https?\:/)\S+
println(“”(https?\\:/)\\S+“”/(https?\\:/)\\S+
println(“”(https?\:/)\S+“”/(https?\:/)\S+

使用双反斜杠或三个引号。在原始代码中使用了双反斜杠,那么为什么还要为
val URL
添加三重引号呢?