Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, regular expressions are powered by the Java regular expression library, as Scala runs on the Java Virtual Machine (JVM). This means that you can utilize Java's Pattern
and Matcher
classes directly. However, Scala also provides its own wrappers and syntactic sugar to make working with regular expressions more idiomatic and straightforward.
Here's a brief guide on working with regular expressions in Scala:
You can define a regular expression using a string and then convert it into a regex pattern using the .r
method.
val numberPattern = "[0-9]+".r
To check if a string matches a given regex pattern:
val isNumber = numberPattern.matches("12345") // true
To find the first match of the regex in a string:
val firstMatch = numberPattern.findFirstIn("Hello 12345 World 67890") println(firstMatch) // Some(12345)
To find all matches:
val allMatches = numberPattern.findAllIn("Hello 12345 World 67890").toList println(allMatches) // List(12345, 67890)
Groups allow you to capture specific portions of your match.
val datePattern = """(\d{4})-(\d{2})-(\d{2})""".r "2023-09-07" match { case datePattern(year, month, day) => println(s"Year: $year, Month: $month, Day: $day") case _ => println("Not a valid date format") }
To replace the first or all occurrences of a regex match in a string:
val replacedFirst = numberPattern.replaceFirstIn("Hello 12345 World 67890", "Number") println(replacedFirst) // Hello Number World 67890 val replacedAll = numberPattern.replaceAllIn("Hello 12345 World 67890", "Number") println(replacedAll) // Hello Number World Number
Scala's pattern matching and case classes mechanism can be leveraged with regular expressions for extraction:
val datePattern(year, month, day) = "2023-09-07" println(s"Year: $year, Month: $month, Day: $day")
In the example above, the values are automatically extracted from the regex groups and assigned to the year
, month
, and day
variables.
Regular expressions in Scala are a powerful tool to search, match, and manipulate strings. Scala provides idiomatic wrappers around Java's regex functionalities, allowing for a more concise and readable syntax, especially when combined with pattern matching and extractors.
Introduction to Regex in Scala:
Regular expressions (regex) are sequences of characters that define a search pattern. In Scala, regex patterns are expressed using the Regex
class.
val pattern: String = "Scala" val regex: Regex = pattern.r val text: String = "Scala is awesome!" println(regex.findFirstIn(text)) // Result: Some(Scala)
Regex Pattern Matching in Scala:
Scala provides pattern matching with regex, allowing you to check if a string matches a pattern.
val pattern: String = "Hello (.*)" val regex: Regex = pattern.r val text: String = "Hello World!" text match { case regex(name) => println(s"Hello, $name") case _ => println("Not a greeting") }
Using Regex in Scala String Manipulation:
Regex can be used for string manipulation, such as finding and replacing patterns.
val pattern: String = "(\\d{2})-(\\d{2})-(\\d{4})" val regex: Regex = pattern.r val text: String = "Date: 01-15-2022" val updatedText: String = regex.replaceAllIn(text, "$2/$1/$3") println(updatedText) // Result: Date: 15/01/2022
Scala Regex API and Methods:
Scala's Regex
class provides various methods for working with regular expressions, such as findFirstIn
, findAllIn
, and replaceAllIn
.
val pattern: String = "\\d+" val regex: Regex = pattern.r val text: String = "There are 42 apples and 7 oranges." val matches: Seq[String] = regex.findAllIn(text).toSeq println(matches) // Result: List(42, 7)
Regex Groups and Capturing in Scala:
Regex groups are portions of the pattern enclosed in parentheses, and they allow capturing specific parts of the matched text.
val pattern: String = "(\\d{2})-(\\d{2})-(\\d{4})" val regex: Regex = pattern.r val text: String = "Date: 01-15-2022" text match { case regex(day, month, year) => println(s"Year: $year, Month: $month, Day: $day") case _ => println("Not a date") }
Character Classes and Ranges in Scala Regex:
Character classes and ranges in regex allow you to match specific characters or a range of characters.
val pattern: String = "[aeiou]" val regex: Regex = pattern.r val text: String = "Regular expressions are powerful!" val vowels: Seq[String] = regex.findAllIn(text).toSeq println(vowels) // Result: List(e, u, a, e, e, i, o, a, i, o, e)
Lookahead and Lookbehind in Scala Regular Expressions:
Lookahead and lookbehind assertions allow you to specify conditions that must be satisfied without consuming characters.
val pattern: String = "\\d+(?=\\syears)" val regex: Regex = pattern.r val text: String = "42 years and 7 months" val matchResult: Option[String] = regex.findFirstIn(text) println(matchResult) // Result: Some(42)
Scala Regex Matchers and Extractors:
Scala provides the Matcher
and Regex.Match
classes for more advanced regex matching and extraction.
val pattern: String = "(\\d+)-(\\w+)" val regex: Regex = pattern.r val text: String = "123-abc" val matchResult: Option[Regex.Match] = regex.findFirstMatchIn(text) matchResult.foreach { matchGroup => val wholeMatch: String = matchGroup.group(0) val numberPart: String = matchGroup.group(1) val wordPart: String = matchGroup.group(2) println(s"Whole match: $wholeMatch, Number: $numberPart, Word: $wordPart") }