Scala 模式语法异常

编程入门 行业动态 更新时间:2024-10-10 21:33:18
本文介绍了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 模式语法异常

本文发布于:2023-11-25 14:57:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   异常   模式   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!