Scala 模式语法异常 [英] Scala Pattern Syntax Exception

查看:48
本文介绍了Scala 模式语法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按字符 "}{" 拆分字符串.但是我收到一个错误:

I'm trying to split a string in by the characters "}{". However I am getting an error:

> val string = "{one}{two}".split("}{")
java.util.regex.PatternSyntaxException: Illegal repetition near index 0
}{
^

我不想使用正则表达式或任何东西.我尝试使用 "\}\{" 并且它也不起作用.

I am not trying to use regex or anything. I tried using "\}\{" and it also doesn't work.

推荐答案

嗯...原因是 split 将其参数字符串视为正则表达式.

Well... the reason is that split treats its parameter string as a regular expression.

现在,{} 都是正则表达式中的特殊字符.

Now, both { and } are special character in regular expressions.

所以你将不得不跳过 split 的参数的正则表达式世界的特殊字符,像这样,

So you will have to skip the special characters of regex world for split's argument, like this,

val string = "{one}{two}".split("\\}\\{")
// string: Array[String] = Array({one, two})

这篇关于Scala 模式语法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆