适用于阶数串变换(Apply several string transformations in s

2019-06-26 02:10发布

我想在斯卡拉功能的方式执行对串几个有序的和连续的replaceAll(...,...)。

什么是最优雅的解决方案? Scalaz欢迎! ;)

Answer 1:

首先,让我们的函数出的replaceAll方法:

scala> val replace = (from: String, to: String) => (_:String).replaceAll(from, to)
replace: (String, String) => String => java.lang.String = <function2>

现在你可以使用Functor的功能例如, 定义在scalaz。 这样,你可以撰写功能,使用map (或使它更好看,使用Unicode的别名)。

它看起来是这样的:

scala> replace("from", "to") ∘ replace("to", "from") ∘ replace("some", "none")
res0: String => java.lang.String = <function1>

如果你喜欢哈斯克尔路撰写(从右到左),使用contramap

scala> replace("some", "none") ∙ replace("to", "from") ∙ replace ("from", "to")
res2: String => java.lang.String = <function1>

你也可以有一些有趣的Category 实例 :

scala> replace("from", "to") ⋙ replace("to", "from") ⋙ replace("some", "none")
res5: String => java.lang.String = <function1>

scala> replace("some", "none") ⋘ replace("to", "from") ⋘ replace ("from", "to")
res7: String => java.lang.String = <function1>

并将其应用于:

scala> "somestringfromto" |> res0
res3: java.lang.String = nonestringfromfrom

scala> res2("somestringfromto")
res4: java.lang.String = nonestringfromfrom

scala> "somestringfromto" |> res5
res6: java.lang.String = nonestringfromfrom

scala> res7("somestringfromto")
res8: java.lang.String = nonestringfromfrom


Answer 2:

如果它只是几个调用,然后只是把它们连。 否则,我想我会尝试一下:

Seq("a" -> "b", "b" -> "a").foldLeft("abab"){case (z, (s,r)) => z.replaceAll(s, r)}

或者,如果你喜欢与混乱通配符和额外的封闭短代码:

Seq("a" -> "b", "b" -> "a").foldLeft("abab"){_.replaceAll _ tupled(_)}


Answer 3:

另一种基于Scalaz,解决这个问题是使用的Endo独异。 此半群捕获恒等函数(作为半群的单位元)和功能的组合物(作为半群的追加操作)。 如果你有功能的任意大小(甚至可能为空)列表中应用此解决方案将是特别有用。

val replace = (from: String, to: String) => (_:String).replaceAll(from, to)

val f: Endo[String] = List(
  replace("some", "none"),
  replace("to", "from"),
  replace("from", "to")    
).foldMap(_.endo)

例如(的folone的例子使用一个)

scala> f.run("somestringfromto")
res0: String = nonestringfromfrom


Answer 4:

定义匿名参数的替换功能,然后你可以连续链条替换功能一起。

scala> val s = "hello world"
res0: java.lang.String = hello world

scala> def replace = s.replaceAll(_, _)
replace: (java.lang.String, java.lang.String) => java.lang.String

scala> replace("h", "H")  replace("w", "W")
res1: java.lang.String = Hello World


Answer 5:

#to replace or remove multiple substrings in scala in dataframe's string column

import play.api.libs.json._
#to find
def isContainingContent(str:String,regexStr:String):Boolean={
  val regex=new scala.util.matching.Regex(regexStr)
  val containingRemovables= regex.findFirstIn(str)
  containingRemovables match{
    case Some(s) => true
    case None => false
  }
}
val colContentPresent= udf((str: String,regex:String) => {
  isContainingContent(str,regex)
})
#to remove
val cleanPayloadOfRemovableContent= udf((str: String,regexStr:String) => {
  val regex=new scala.util.matching.Regex(regexStr)
  val cleanedStr= regex.replaceAllIn(str,"")
  cleanedStr
})
#to define
val removableContentRegex=
"<log:Logs>[\\s\\S]*?</log:Logs>|\\\\n<![\\s\\S]*?-->|<\\?xml[\\s\\S]*?\\?>"

#to call
val dfPayloadLogPresent = dfXMLCheck.withColumn("logsPresentInit", colContentPresent($"payload",lit(removableContentRegex)))
val dfCleanedXML = dfPayloadLogPresent.withColumn("payload", cleanPayloadOfRemovableContent($"payload",lit(removableContentRegex)))


文章来源: Apply several string transformations in scala