Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 替换字符串中除Scala中第一次出现之外的元素_Regex_Scala - Fatal编程技术网

Regex 替换字符串中除Scala中第一次出现之外的元素

Regex 替换字符串中除Scala中第一次出现之外的元素,regex,scala,Regex,Scala,我对正则表达式比较陌生,虽然我知道Scala有一种在第一次出现之前使用替换项的方法,但是我想知道是否有一种方法可以在字符串中的项第一次出现在Scala中之后去掉它 例如,如果我有一个字符串 你好W.o.r.l.d不需要华丽的正则表达式。只需分离,然后重新连接 val noDots = "Hello. W.o.r.l.d".split("\\.") val res = noDots.head + "." + noDots.tail.mkString //res: String = Hello.

我对正则表达式比较陌生,虽然我知道Scala有一种在第一次出现之前使用替换项的方法,但是我想知道是否有一种方法可以在字符串中的项第一次出现在Scala中之后去掉它

例如,如果我有一个字符串


你好W.o.r.l.d不需要华丽的正则表达式。只需分离,然后重新连接

val noDots = "Hello. W.o.r.l.d".split("\\.")
val res = noDots.head + "." + noDots.tail.mkString  //res: String = Hello. World

如果只有一个,这将正常工作。在文本中,但如果没有。然后在字符串的末尾插入一个。不需要特殊的正则表达式。只需分离,然后重新连接

val noDots = "Hello. W.o.r.l.d".split("\\.")
val res = noDots.head + "." + noDots.tail.mkString  //res: String = Hello. World

如果只有一个,这将正常工作。在文本中,但如果没有。然后在字符串的末尾插入一个。这里有一种混合使用正则表达式模式匹配和替换的方法:


请注意,如果字符串没有,它将由其他大小写匹配。

这里有一种混合使用正则表达式模式匹配和替换的方法:


请注意,如果字符串没有,它将由大小写匹配。

您也可以尝试使用indexOf和substring组合

scala> val x = "Hello. W.o.r.l.d"
x: String = Hello. W.o.r.l.d

scala> x.substring(0,x.indexOf(".")+1) + x.split("\\.").tail.mkString
res21: String = Hello. World

scala>

您还可以尝试使用indexOf和substring组合

scala> val x = "Hello. W.o.r.l.d"
x: String = Hello. W.o.r.l.d

scala> x.substring(0,x.indexOf(".")+1) + x.split("\\.").tail.mkString
res21: String = Hello. World

scala>