Open In App

Regular Expressions in Scala

Improve
Improve
Like Article
Like
Save
Share
Report

Regular Expressions explain a common pattern utilized to match a series of input data so, it is helpful in Pattern Matching in numerous programming languages. In Scala Regular Expressions are generally termed as Scala Regex.
Regex is a class which is imported from the package scala.util.matching.Regex and it is extensively applied in searching and text parsing. In order to recast a string into a Regular Expressions, we need to make use of r() method with the stated string.

Example :




// Scala program for Regular 
// Expressions
  
// Creating object
object GfG 
{
  
    // Main method
    def main(args: Array[String]) 
    {
      
        // Applying r() method
        val portal = "GeeksforGeeks".r
        val CS = "GeeksforGeeks is a CS portal."
  
        // Displays the first match
        println(portal findFirstIn CS)
    }
}


Output:

Some(GeeksforGeeks)

Here, we have called the method r() on the stated string to obtain an instance of Regex class, in order to create a pattern. The method findFirstIn() is utilized in the above code to find the first match of the Regular Expression. In order to find all the matching word in the expression, use findAllIn() method.

Example :




// Scala program for Regular 
// Expressions
import scala.util.matching.Regex
  
// Creating object
object GfG 
{
  
    // Main method
    def main(args: Array[String]) 
    {
      
        // Applying Regex class
        val x = new Regex("Nidhi")
        val myself = "My name is Nidhi Singh."
  
        // replaces first match with the
        // String given below
        println(x replaceFirstIn(myself, "Rahul"))
    }
}


Output:

My name is Rahul Singh.

We can even use constructor of Regex in place of r() method. Here, replaceFirstIn() method is utilized to replace the first match of the stated string and we can even replace all the matches using replaceAllIn() method.

Example :




// Scala program for Regular 
// Expressions
import scala.util.matching.Regex
  
// Creating object
object GfG 
{
  
    // Main method
    def main(args: Array[String]) 
    {
      
        // Applying Regex class
        val Geeks = new Regex("(G|g)fG")
        val y = "GfG is a CS portal. I like gfG"
  
        // Displays all the matches separated
        // by a separator
        println((Geeks findAllIn y).mkString(", "))
    }
}


Output:

GfG, gfG

Here, we have used mkString method to concatenate all the matches, separated by a separator and a pipe ( | ) is used in the above code to search both upper and lower case in the string given. Thus, both upper and lower case of the stated string is returned here.

 

Syntax for Scala Regular Expressions

Java inherits some features from Perl, Scala inherits the syntax of Scala regex from Java. Here below is the list of metacharacter syntax:

Subexpression Matches
^ It is used to match starting point of the line.
$ It is used to match terminating point of the line.
. It is used to match any one character excluding the newline.
[…] It is used to match any one character within the brackets.
[^…] It is used to match any one character which is not in the brackets.
\\A It is used to match starting point of the intact string.
\\z It is used to match terminating point of the intact string.
\\Z It is used to match end of the whole string excluding the new line, if it exists.
re* It is utilized to match zero or more appearances of the foregoing expressions.
re+ It is used to match one or more of the foregoing expressions.
re? It is used to match zero or one appearance of the foregoing expression.
re{ n} It is used to matches precisely n number of appearances of the foregoing expression.
re{ n, } It is used to match n or more appearances of the foregoing expression.
re{ n, m} It is used to match at least n and at most m appearances of the foregoing expression.
q|r It is utilized to match either q or r.
(re) It is utilized to group the Regular expressions and recollects the text that are matched.
(?: re) It also groups the regular expressions but does not recollects the matched text.
(?> re) It is utilized to match self-reliant pattern in absence of backtracking.
\\w It is used to match characters of the word.
\\W It is used to match characters of the non-word.
\\s It is utilized to match white spaces which are analogous to [\t\n\r\f].
\\S It is used to match non-white spaces.
\\d It is used to match the digits i.e, [0-9].
\\D It is used to match non-digits.
\\G It is used to match the point where the endmost match overs.
\\n It is used for back-reference to occupy group number n.
\\b It is used to match the word frontiers when it is out of the brackets and matches the backspace when it is in the brackets.
\\B It is used to match non-word frontiers.
\\n, \\t, etc. It is used to match the newlines, tabs, etc.
\\Q It is used to escape (quote) each of the characters till \\E.
\\E It is used in ends quoting starting with \\Q.


Last Updated : 08 Apr, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads