如何在scala中的单词后截断字符串

时间:2014-02-28 19:32:20

标签: regex scala

给出以下字符串......

"localhost:9000/one/two/three"

我想在单词two后截断并获取

"localhost:9000/one/two"

我已经实现了truncateBeforetruncateAfter这样的方法:

def truncateBefore(s: String, p: String) = {
  s.substring(s.indexOf(p) + p.length, s.length)
}

def truncateAfter(s: String, p: String) = {
  s.substring(0, s.indexOf(p) + p.length)
}

这些方法起作用并返回预期结果:

scala> truncateAfter("localhost:9000/one/two/three", "three")
res1: String = localhost:9000/one/two

scala> truncateBefore("localhost:9000/one/two/three", "three")
res2: String = /three

在scala中有更好的方法吗?最好是正则表达式?

5 个答案:

答案 0 :(得分:6)

使用正则表达式的一个选项:

val beforeAfter = "(^.*two)(.*)$".r

scala> val beforeAfter(after, before) = "localhost:9000/one/two/three"
after: String = localhost:9000/one/two
before: String = /three

使用拆分的另一个选项:

scala> "localhost:9000/one/two/three" split ("two")
res0: Array[java.lang.String] = Array(localhost:9000/one/, /three)

如果您在输入中没有单词two,这些不是超级强大的解决方案,但您可以相应地处理它...

中使用正则表达式进行理解:

scala> val beforeAfter = "(^.*two)(.*)$".r
beforeAfter: scala.util.matching.Regex = (^.*two)(.*)$

scala> (for {
     |   matches <- beforeAfter.findAllIn("localhost:9000/one/two/three").matchData
     |   tokens <- matches.subgroups
     |  } yield tokens).toList
res0: List[String] = List(localhost:9000/one/two, /three)

如果找不到匹配项,则是安全的:

scala> (for {
     |   match <- beforeAfter.findAllIn("localhost").matchData
     |   token <- match.subgroups
     |  } yield token).toList
res1: List[String] = List()

答案 1 :(得分:2)

在第一个文字之后拆分,没有太多的正则表达式(双关语)。

scala> implicit class `split after`(val s: String) {
     | def splitAfter(p: String): (String, String) = {
     |   val r = (Regex quote p).r
     |   r findFirstMatchIn s map (m => (s.substring(0, m.end), m.after.toString)) getOrElse (s, "")
     | }}
defined class split$u0020after

scala> "abcfoodeffooghi" splitAfter "foo"
res2: (String, String) = (abcfoo,deffooghi)

scala> "abc*def" splitAfter "*"
res3: (String, String) = (abc*,def)

答案 2 :(得分:1)

显然效率不高,但确实有效。

scala> val url = "localhost:9000/one/two/three"
url: String = localhost:9000/one/two/three

scala> url.reverse.dropWhile(_ != '/').reverse
res0: String = localhost:9000/one/two/

使用索引:

scala> url.drop(url.reverse.indexOf('/'))
res1: String = host:9000/one/two/three

答案 3 :(得分:1)

好的,感谢大家......这是我的解决方案:

package object typeExtensions {

  implicit class StringExtensions(val string: String) extends AnyVal {

    def truncateAfter(pattern: String) = beforeAfter(pattern).map(_._1).getOrElse(string)
    def truncateBefore(pattern: String) = beforeAfter(pattern).map(_._2).getOrElse(string)

    private def beforeAfter(pattern: String) = {
      if (string.contains(pattern)) {
        val beforeAfter = ("(^.*" + Pattern.quote(pattern) + ")(.*)$").r
        val beforeAfter(before, after) = string
        Some(before, after)
      } else None
    }
  }
}

任何改进上述代码的建议都非常受欢迎; - )

答案 4 :(得分:0)

Scala 2.13开始,作为正则表达式解决方案的替代方案,还可以通过unapplying a string interpolator模式匹配String

"localhost:9000/one/two/three" match {
  case s"${prefix}two${suffix}" => s"${prefix}two"
  case _                        => "no match"
}
// String = "localhost:9000/one/two"