给定一个String,我需要在开头找出原始String中包含任何的最大String ,但在看到定义的模式时停止。
例如,给定" \ n www。{Google} .comcomxxxcom"作为字符串和" com"作为结束模式,
我们想要" \ n www。{Google}。"结果。
我尝试做类似
的事情val filter = """[\\s\\S]*(?!com)""".r
filter.findAllIn("\nwww.{Google}.com").toArray
但结果不是我想要的。
我也尝试了其他可能性但失败了。有人可以帮忙吗?
答案 0 :(得分:0)
您可以使用
val filter = """^[\s\S]+?(?=com)""".r
val s = "\nwww.{Google}.comcomxxxcom"
print(filter.findFirstIn(s))
// => Some(
www.{Google}.)
请参阅Scala demo
<强>详情
^
- 字符串锚的开头[\s\S]+?
- 第1组:任意1个字符,尽可能少,直到第一个com
- 匹配并消费。请注意,由于您需要来自字符串的单个匹配,因此您可以使用findFirstIn
而不是findAllIn
。