Regular Expressions and Pattern Matching

Let’s learn about regular expressions and pattern matching in Go.

What is pattern matching?

Pattern matching is a technique for searching a string for some set of characters based on a specific search pattern that is based on regular expressions and grammar.

What is a regular expression?

A regular expression is a sequence of characters that defines a search pattern. Every regular expression is compiled into a recognizer by building a generalized transition diagram called a finite automaton. A finite automaton can be either deterministic or nondeterministic. Nondeterministic means that more than one transition out of a state can be possible for the same input. A recognizer is a program that takes a string x as input and is able to tell whether x is a sentence of a given language.

What is a formal grammar?

A grammar is a set of production rules for strings in a formal language—the production rules describe how to create strings from the alphabet of the language that are valid according to the syntax of the language. Grammars don’t describe the meaning of a string or what can be done with it in whatever context—they only describe its form. What is important here is to realize that grammars are at the heart of regular expressions because, without grammars, we can’t define or use a regular expression.

So, you might wonder why we’re talking about regular expressions and pattern matching in this chapter. The reason is simple. In a while, we will learn how to store and read CSV data from plain text files, and we should be able to tell whether the data we are reading is valid or not.

About Go regular expressions

We begin our discussion about Go regular expressions by presenting some common match patterns used for constructing regular expressions.

Get hands-on with 1200+ tech skills courses.