【问题标题】:Why doesnt look ahead and look behind Regex work in Kotlin?为什么不向前看,向后看正则表达式在 Kotlin 中的工作?
【发布时间】:2016-01-25 21:14:35
【问题描述】:

例子:

    // Java
    System.out.println("one;two;th/;ree".split("(?<!/);").length);  // 3

    // Kotlin
    println("one;two;th/;ree".split("(?<!/);").size) // 1

如何解决这个问题?

【问题讨论】:

标签: regex kotlin


【解决方案1】:

在您的 Kotlin 示例中,您不是按 Regex 而是按 String 拆分的。

尝试以下方法:

println("one;two;th/;ree".split(Regex("(?<!/);")).size) // 3

【讨论】:

  • 或者你也可以使用String.toRegex扩展函数,我觉得它更易读:println("one;two;th/;ree".split("(?&lt;!/);".toRegex()).size)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 2012-04-18
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多