Suppose you have a simple date string: "2015-02-27"

Regular expressions allow to parse the date and extract parts thereof: (dddd)-(dd)-(dd) defines a pattern which matches the digits and defines capturing groups for the year, month, and day parts.

In Java, a simple example usage would look like this:

Pattern pattern = Pattern.compile("(dddd)-(dd)-(dd)");
Matcher matcher = pattern.matcher("2015-02-27");
while (matcher.find()) {
    System.out.println("Year: " + matcher.group(1));
    System.out.println("Month: " + matcher.group(2));
    System.out.println("Day: " + matcher.group(3));
}

Accessing the groups by index is rather awkward and error prone. Luckily, Java 7 introduced support for named groups:

  Pattern pattern = Pattern.compile("(?<year>dddd)-(?<month>dd)-(?<day>dd)");
Matcher matcher = pattern.matcher("2015-02-27");
while (matcher.find()) {
    System.out.println("Year: " + matcher.group("year"));
    System.out.println("Month: " + matcher.group("month"));
    System.out.println("Day: " + matcher.group("day"));
}

That’s better, but I personally don’t like how the group names pollute the pattern. Regular expressions are hard to read already, the Java escape characters add yet more clutter, and the additional ?<group> constructs are a kiss of death.

Let’s look at how the same problem can be tackled in Scala:

According to the docs, the canonical way to create a Regex is by using the method r, which is provided implicitly for strings:

val Date = """(dddd)-(dd)-(dd)""".r

Note the triple quotes, which allow to use the backslashes without further escaping. While this is nice, the true advantage of Scala is that regular expressions can be used as extractors in a pattern match. The pattern stays plain and simple, and yet we have all the freedom to name groups as we please:

"2015-02-27" match {
  case Date(year, month, day) =>
    println(s"Year: $year")
    println(s"Month: $month")
    println(s"Day: $day")
}

Look ma, no clutter!

 

Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors Regex Extractors 

Regex Extractors 

A Scala Gem for You: Cleaning Up with Regex Extractors

About The Author
- Java & Scala software craftswoman, mother of two, mountain biker, dog lover, guitar player, country bumpkin — in no particular order. Read more on my blog: http://netzwerg.ch/

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>