Regular Expressions in Scala

Scala | Regular Expressions: Here, we will learn about Regular expressions in Scala, how to create them, and use then in your program?
Submitted by Shivang Yadav, on September 21, 2020

Regular Expression

Regular expression is a sequence of characters that are used to create a pattern that is used for searching and matching in strings.

These search patterns are used in programming to check for the presence of a pattern or character or string in the given string for validation and checks.

In Scala, regular expression (regex) is created using r method which is a method of the Regex class which is imported using the package scala.util.matching.Regex.

Syntax for creating a regular expression:

val  regex = ""string".r

Program to create a regular expression

object MyClass {
    def main(args: Array[String]) {
        var pattern = "@".r;
        println("Regular Expression :" + pattern)
    }
}

Output:

Regular Expression : @

You can check if a regular expression is present in a string, match patterns using findAllIn method, findFirstIn method.

Program to check for the presence of a regular expression in a string

object MyClass {
    def main(args: Array[String]) {
        var pattern = "programming".r
        val string = "Hello! learn programming at inclduehelp.com"

        println((pattern findAllIn string).mkString(" "))
    }
}

Output:

Programming

Here, we have used the findAllIn method to find all the matching patterns in the string. The method returns an iterator, so for converting an iterator to a string be have used the mkString method.

There are some other functions like findFirstIn that are used to find pattern in Scala.  

You can also replace the found pattern in a string in Scala using replaceFirstIn() and replaceAllIn() methods.

Creating a Regular Expression (regex) in Scala

In Scala, we can create our own regular expressions using the java creation syntax. There are some meta character syntaxes that are used to create regular expressions,

Subexpression Working
^ This is used to match the pattern to the start of the line.
$ This is used to match the pattern to the end of the line.
. This is used to match a single character in the line until the end of line occurs.
[...] This is used to match for the character that is inside the brackets.
[^...] This is used to match for the character that is not inside the brackets
\\A It is used to match at the beginning of the string.
\\z It is used to match at the end of the string.
re* Matches the occurrence of character with 0 or more frequency.
Example: i* will check for '', i, ii, iii..
re+ Matches the occurrence of character with 1 or more frequency.
Example: i+ will check for i, ii, iii.
re? Matches the occurrence of character with 0 or 1 frequency.
Example: i? will check for'', i.
re{n} Matches the occurrence of character with n frequency.
Example: i{3} will check for iii.
re{n,} Matches the occurrence of character with n or more frequency.
Example: i{4, } will check for iiii,iiiii, iiiiii...
re{n, m} Matches the occurrence of character with n to m frequency.
Example: i{2,4} will check for ii, iii, iiii.
a|b It is used to find the match of one of the two patterns a or b.
(re) It is used to create a group of regular expressions and remember all the matched parts of the string.
(?:re) It is used to create a group of regular expressions
(?>re) It is used to create an independent group of matched expressions.
\\w It is used to match word character in the string.
\\W It is used to match non-word characters in the string.
\\s It is used to match all the whitespace in the string.
\\S It is used to match all the non-whitespace in the string.
\\d It used to match digits (all 0-9) in the string.
\\D It is used to match non-digits in the string.
\\G It is used to match point where the last match was finished.
\\n It is back-reference to capture group number 'n'.
\\Q It used to create an escape for characters until \\E is encountered.
\\E It ends the escape that was initiated using \\Q.

Now, we know all about the expressions used to create a regular expression in Scala. Bookmark this page as the references are not quite easy to remember and you will need to refer it back while working with string in Scala.

Let's take a few examples that will make the process and above-mentioned expressions clear.

  1. Match a 10 digit number - \\d{10}
  2. Match a valid email address - (@[a-z]+.com)
  3. Math Scala in all cases - ([Ss]cala(, )?)+
  4. Match all vowels - [aeiouAEIOU]
  5. Match non-digits in the string - [0-9]


Comments and Discussions!

Load comments ↻





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