Home »
Scala
Regular Expressions in Scala
By IncludeHelp Last updated : October 20, 2024
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.
Creating Regular Expression
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
Syntax for creating a regular expression –
val regex = ""string".r
Examples of Regular Expression
Here are some practical examples that demonstrate the use of regular expressions for pattern matching and text manipulation –
Example 1: 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.
Example 2: Check for the presence of a regular expression
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.
Meta Characters and Syntax for Creating Regular Expressions
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.
Quick Examples
Let's take a few examples that will make the process and above-mentioned expressions clear.
- Match a 10 digit number - \\d{10}
- Match a valid email address - (@[a-z]+.com)
- Math Scala in all cases - ([Ss]cala(, )?)+
- Match all vowels - [aeiouAEIOU]
- Match non-digits in the string - [0-9]