有没有办法用Scala的Regex库在2个字符串之间返回不匹配字符串的第一个实例?
例如:
val a = "some text abc123 some more text"
val b = "some text xyz some more text"
a.firstUnmatched(b) = "abc123"
答案 0 :(得分:1)
正则表达式适合匹配&根据模式替换字符串。
但要寻找字符串之间的差异?不完全是。
但是,object Main extends App {
val a = "some text abc123 some more text 321abc"
val b = "some text xyz some more text zyx"
val firstdiff = (a.split(" ") diff b.split(" "))(0)
println(firstdiff)
}
可用于查找差异。
val reg = "\\w+".r
val firstdiff = (reg.findAllIn(a).toList diff reg.findAllIn(b).toList)(0)
打印“abc123”
毕竟需要正则表达式吗?然后意识到分裂可以用正则表达式匹配来代替 此示例中的正则表达式模式查找单词:
{{1}}