Scala Regex String Extraction

Introduction

— Joe Baker, Manager Software Engineering

From time to time the Engineering, Operations, and Security groups at Threat Stack contribute blog posts that share information on techniques and tools we’ve developed so we can do things faster, more accurately, and with fewer resources. These range from tips for using Scala in the real world, to improving our SOC 2 management process using a home-grown tool called sockembot, to insights into how we manage our on-call rotation using another home-built tool called Deputize (which we’ve since made available as open source).

Today’s post is by Alfredo Perez, one of our software engineers, and focuses on Scala Regex String Extraction.

One of my favorite Scala patterns that I’ve learned and used here at Threat Stack is Regex String Extraction with pattern matching. It’s a simple pattern but very powerful for extracting parts of a string and very readable. The power comes from the use of regular expression groups combined with the pattern matching of Scala.

I’ve used this pattern to help solve many problems for Threat Stack from web scraping vulnerability data to matching authentication header values. In the following example, I use it to extract some values typically found in request and response headers. Something like the following:

Wrapping Up . . .

Hopefully the preceding examples were helpful and gave some insight into how powerful and usable the Regex String Extraction is. Whether you’re conducting searches, making updates, or carrying out validations, you should find it highly usable and a great way to save time and effort.