How to replace a regular expression pattern in a string in Scala?

Here, we are going to learn how to replace a regular expression pattern in a string in Scala programming language?
Submitted by Shivang Yadav, on April 26, 2020 [Last updated : March 10, 2023]

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() 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()

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() Method

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

Syntax:

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

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

Program to replace regular expression pattern in a string using 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() 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.

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()

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() 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.

The only difference is the syntax.

Syntax:

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

Program to replace regular expression pattern in a string using 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'

Scala String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.