Title: Extract Substrings Using Regex
Slug: extract_substring_using_regex
Summary: Using Scala.
Date: 2017-01-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon
This tutorial was inspired by the awesome Scala Cookbook.
In [39]:
// Create a string value
val text: String = "27 aircraft"
In [40]:
// Create a regex with two pattern matches (one number and one word)
val pattern = "([0-9]+) ([A-Za-z]+)".r
In [41]:
// Apply the regex pattern such that each of the two pattern matches is assigned to a seperate value
val pattern(vehicle_number, vehicle_type) = text
In [42]:
// View the value
vehicle_number
Out[42]:
In [43]:
// View the value
vehicle_type
Out[43]: