scala字符串替换_如何在Scala中替换字符串中的正则表达式模式?

scala字符串替换

Scala | 替换字符串中的正则表达式模式 (Scala | Replacing a regular expression pattern in a string)

Replacing a part of the string that matches our given regular expression can be done using multiple methods.

可以使用多种方法替换匹配给定正则表达式的字符串部分

As strings are immutable you cannot replace the pattern in the string itself instead, we will be creating a new string that stores the updated string.

由于字符串是不可变的,因此您不能替换字符串本身中的模式,我们将创建一个新字符串来存储更新后的字符串。

1)replaceAll()方法 (1) replaceAll() Method)

The method replaces all the occurrences of the pattern matched in the string.

该方法替换字符串中所有匹配的模式。

Syntax:

句法:

    string.replaceAll("regex", "replaceString")

Program to replace regular expression pattern in a string using replaceAll()

程序使用replaceAll()替换字符串中的正则表达式模式

object MyClass {
def main(args: Array[String]) {
val myString = "i can ride at a speed of 190 KmpH"
println("The string is '" + myString + "'")
println("Replacing all digits of the string with '*'")
val updatedString = myString.replaceAll("[0-9]+", "*")
println("Updated string is' " + updatedString + "'")
}
}

Output

输出量

The string is 'i can ride at a speed of 190 KmpH'
Replacing all digits of the string with '*'
Updated string is' i can ride at a speed of * KmpH'

2)replaceAllIn()方法 (2) replaceAllIn() Method)

The method does the same work as replaceAll() but has a different syntax.

该方法的功能与replaceAll()相同,但是语法不同。

Syntax:

句法:

    regex.replaceAllIn("String", "replacestring")

The function also returns an updated string with the matched pattern changed with the given replacestring.

该函数还返回一个更新的字符串,其中匹配的模式已更改为给定的replacestring

Program to replace regular expression pattern in a string using replaceAllIn()

程序使用replaceAllIn()替换字符串中的正则表达式模式

object MyClass {
def main(args: Array[String]) {
val myString = "I can ride At the Speed of 190 KmpH"
val pattern = "[A-Z]".r
println("The string is '" + myString + "'")
println("Replacing all uppercase characters of the string with '*'")
val updatedString = pattern.replaceAllIn(myString, "*")
println("Updated string is '" + updatedString + "'")
}
}

Output

输出量

The string is 'I can ride At the Speed of 190 KmpH'
Replacing all uppercase characters of the string with '*'
Updated string is '* can ride *t the *peed of 190 *mp*'

3)replaceFirst()方法 (3) replaceFirst() Method)

The replaceFirst() method will replace the match pattern in the string but only at its first occurrence i.e. the match pattern will be replaced only at its first occurrence.

replaceFirst()方法将替换字符串中的匹配模式,但仅在其首次出现时进行替换,即,匹配模式将仅在其首次出现时进行替换。

Syntax:

句法:

    string.replaceFirst("regex", "replaceString")

The function will return an updated string with the first matched pattern replaced with the given replace string.

该函数将返回一个更新的字符串,其中第一个匹配的模式将替换为给定的替换字符串。

Program to replace regular expression pattern in a string using replaceFirst()

程序使用replaceFirst()替换字符串中的正则表达式模式

object MyClass {
def main(args: Array[String]) {
val myString = "i can ride at a speed of 190 KmpH"
println("The string is '" + myString + "'")
println("Replacing first digits of the string with '*'")
val updatedString = myString.replaceFirst("[0-9]", "*")
println("Updated string is '" + updatedString + "'")
}
}

Output

输出量

The string is 'i can ride at a speed of 190 KmpH'
Replacing first digits of the string with '*'
Updated string is 'i can ride at a speed of *90 KmpH'

4)replaceFirstIn()方法 (4) replaceFirstIn() Method)

The replaceFirstIn() method will replace the match pattern in the string but only at its first occurrence i.e. the match pattern will be replaced only at its first occurrence.

replaceFirstIn()方法将替换字符串中的匹配模式,但仅在其首次出现时进行替换,即,匹配模式将仅在其首次出现时进行替换。

The only difference is the syntax.

唯一的区别是语法。

Syntax:

句法:

    regex.replaceFirstIn("string", "replaceString")

Program to replace regular expression pattern in a string using replaceFirstIn()

程序使用replaceFirstIn()替换字符串中的正则表达式模式

object MyClass {
def main(args: Array[String]) {
val myString = "I can ride At the Speed of 190 KmpH"
val pattern = "[A-Z]".r
println("The string is '" + myString + "'")
println("Replacing first occurence of uppercase characters of the string with '*'")
val updatedString = pattern.replaceFirstIn(myString, "*")
println("Updated string is '" + updatedString + "'")
}
}

Output

输出量

The string is 'I can ride At the Speed of 190 KmpH'
Replacing first occurence of uppercase characters of the string with '*'
Updated string is '* can ride At the Speed of 190 KmpH'

翻译自: https://www.includehelp.com/scala/how-to-replace-a-regular-expression-pattern-in-a-string-in-scala.aspx

scala字符串替换

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380474.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

有趣的数

描述 把分数按下面的办法排成一个数表。 ① ② ⑥ ⑦ 1/1 1/2 1/3 1/4… ③ ⑤ ⑧ 2/1 2/2 2/3… ④ ⑨ 3/1 3/2 … ⑩ 4/1… … 我们以z字型方法给上表的每项编号。特定方法:第一项是1/1,然后是1/2、2/1、3/1、2/2、1/3、1/4、2/3……。编程输入项号N&…

linux系统的层次结构,关于Linux操作系统层次结构分析

本文转自http://www.jb51.net/LINUXjishu/214104.html首先来看一张图(这是Linux操作系统的大致层次结构):最内层是硬件,最外层是用户常用的应用,比如说firefox浏览器,evolution查看邮件,一个计算流体模型等等。硬件是物…

Windows Vista版本比较

Windows Vista上市在即,对许多朋友来说目前急待解决的问题便是,在升级或购买时应该选择Windows Vista的哪个版本,哪个版本更契合自己的应用需求,究竟应该选择Windows Vista Home呢还是Windows Vista Ultimate?等等。 之前在Windo…

shellcode---c和汇编混合编程---弹出cmd

首先用C/C语言实现弹出cmd #include "stdio.h" #include "windows.h"int main(int argc, char* argv[]) {printf("begin\n");HINSTANCE libHandle;char *dll"kernel32.dll";libHandleLoadLibrary(dll);WinExec("cmd.exe",S…

ajax 复制到“剪贴板”

有时候可能会做一些“复制”按钮的功能&#xff0c;当用户点击“复制”按钮时&#xff0c;就会将要复制的内容复制出来&#xff0c;以下代码即实现“复制”按钮的功能。该功能需要用到AJAX的PageMethods来调用页面后台代码来实现。 调用PageMethods&#xff0c;需要引用 <sc…

找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串

找到最大回文子串Problem statement: 问题陈述&#xff1a; Given a string, you have to find the largest palindromic substring using O(1) space complexity. 给定一个字符串&#xff0c;您必须使用O(1)空间复杂度找到最大的回文子字符串。 Input:T Test caseT no of in…

日期计算

描述 如题&#xff0c;输入一个日期&#xff0c;格式如&#xff1a;2010 10 24 &#xff0c;判断这一天是这一年中的第几天。 输入 第一行输入一个数N&#xff08;0< N<100&#xff09;,表示有N组测试数据。后面的N行输入多组输入数据&#xff0c;每行的输入数据都是一个…

c语言程序设计编程解读,【答题】C语言程序设计问题与解释实验

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include#define N 13main(){int y,m,D,q,t0,i,day0,a0,Day,n,k,O[N]{0,31,29,31,30,31,30,31,31,30,31,30,31},p[N]{0,31,28,31,30,31,30,31,31,30,31,30,31};//y是年&#xff0c;m是月&#xff0c;D是日&#xff0c;q计算周几&am…

blog的转变

从技术为主的blog&#xff0c;变为记录生活琐事的blog&#xff0c;OMG&#xff01;天气晴朗&#xff0c;不过心情并不怎么轻松&#xff0c;论文下周必须要写完&#xff0c;同时留校结果也就该出来了。曾经一度以为&#xff0c;一个人最快乐自由。不过不得不承认&#xff0c;或许…

没有值的json字符串_Java中具有原始数据类型值的字符串连接

没有值的json字符串Given a string and some of the primitive data type values, we have to concatenate them with the string in Java. 给定一个字符串和一些原始数据类型值&#xff0c;我们必须将它们与Java中的字符串连接起来。 In the example below, we have declared…

a letter and a number(一封信和一个数字)

描述 we define f(A) 1, f(a) -1, f(B) 2, f(b) -2, … f(Z) 26, f(z) -26; Give you a letter x and a number y , you should output the result of yf(x). 我们定义f ( A ) 1&#xff0c;f ( A ) - 1&#xff0c;f ( B ) 2&#xff0c;f ( B ) - 2&#xff0c;…

c语言初始化字符数组为空,怎么把已经初始化的字符数组设置为空?

怎么把已经初始化的字符数组设置为空&#xff1f;两种方法&#xff0c;如下所示。方法一&#xff1a;代码演示#include #include int main(void){char a[]{x,y,z}; //定义字符数组&#xff0c;并初始化int i0;for(i0;i<3;i)printf("%c\t",a[i]); //输出初始化的数…

C和汇编混合编程---栈平衡

最近在搞C和汇编混合编程&#xff0c;对栈平衡有点小理解&#xff0c;记录一下 当我们调用一个API或者子程序时时&#xff0c;API和子程序可以理解为函数&#xff0c;我们不必在返回的时候平衡栈里面的函数参数&#xff0c;但C语言库函数要我们自己平衡栈数据&#xff0c; 比如…

[导入]2006年10大变态站名网站排名

作者&#xff1a; 马国良 | 2006年11月14日13时31分 | 【内容提要】第一名&#xff1a;妈妈说…… 入选原因&#xff1a;单看了两个域名就让人觉得变态“妈妈说就算你注册的域名再长google都能搜索出来”(mamashuojiusuannizhucedeyumingzaichanggoogledounengsousuochulai.cn)…

如何创建Java程序

(1),点击 (2),点击OK (3),File->New->Project… (4),Java Project->Next> (5),Project name随便填&#xff08;这里以qweqwe为例&#xff09; 然后Finish (6),鼠标右击qweqwe&#xff08;随便名称都可以&#xff09;->New->Class (7),同理&#xff0c;…

改变地址栏图标

<link rel"icon" href"/favicon.ico" type"image/x-icon" />转载于:https://www.cnblogs.com/heshuiping/archive/2011/06/04/2072515.html

在JavaScript中以日期/月/年格式获取当前日期

在JavaScript中获取当前日期 (Getting current date in JavaScript) To get the current date in JavaScript, we need to use three library functions of Date class, 要在JavaScript中获取当前日期 &#xff0c;我们需要使用Date类的三个库函数&#xff0c; Date getDate()…

c语言数据转移,重温C语言(2)之数据

一 数据类型数据就是代表某些信息的数字和字符。按计算机的储存方式可分为两大基本类型&#xff1a;整数类型和浮点数类型。1 关键字初始C90C99intsigned_Boollongvoid_Complexshort_Imaginaryunsignedcharfloatdouble2 存储单元首先得记住&#xff0c;计算机内部都是以二进制进…

对esp和ebp分析来了解函数的调用过程

esp&#xff1a;扩展栈指针寄存器&#xff0c;是指针寄存器的一种&#xff0c;用于存放函数栈顶指针&#xff08;栈顶指针&#xff09; ebp&#xff1a;扩展基址指针寄存器&#xff0c;也被称为帧指针寄存器&#xff0c;用于存放函数栈底指针&#xff08;栈底指针&#xff09;。…

放大镜,缩小镜

今天看了 CSDN上的很多文章&#xff0c;有很经典的&#xff0c;也有比较一般的&#xff0c;加上昨天我们老大发给我看的一些CEO的讲话内容的文章&#xff0c;突然发现&#xff0c;不知道是这个行业内部的问题还是整个国内评论家门的问题&#xff0c;在讨论和研究一个问题的时候…