Regular expressions in Go

Hermann Rösch
4 min readSep 9, 2022

--

What are regular expressions?

Regular expressions can seem like random gibberish at first glance — they look like the result of a cat walking over a keyboard ⌨️🐈!

However, as weird or as complex as regexps might look, they are one of the most valuable tools in string processing. In short, a regular expression is a sequence of symbols and characters expressing a pattern to search within a piece of text.

Regexps may be regarded as a sublanguage most programming languages support, but note that the regexp syntax might vary slightly from language to language.

Why do we need regular expressions?

Now that you know what regular expressions are, you might think — why do we need such complex patterns?

An everyday use case of regexps you might have already used without knowing in-depth about them is searching for files with the same extension on your computer. For example, in both Windows & macOS, you might have written the *.pdf expression in the search bar of the file explorer to find all files on your computer with the .pdf extension.

Of course, regexps are not limited to just file searching! They are also used to extract or find e-mail addresses, names, dates, or any other structured and standard pattern from a piece of text.

Learning regexps at JetBrains Academy

If you’re keen on learning more about regexps, you can take a look at two great topics at JetBrains Academy on Hyperskill about regular expressions:

Also, you can learn how to create a simple CLI application to test regexps in Go with the Regex Engine project! It will teach you the syntax of regular expressions and how to use recursion to create a basic regular expression validator!

And if you want a quick intro on how to use regexps in Go and learn about more real-life use cases for regexps, keep reading!

Writing a regular expression in Go

Go’s standard library provides the regexp package; it allows you to create regexp patterns via the following syntax:

The regex.Compile() function creates a regexp that you can further match to a string via the re.MatchString() method. An important detail is that you must write regexp patterns in Go within backticks: `pattern`

Take notice that the above regexp only matches the hello string; regular expressions are case sensitive, so the Hello string doesn’t return a match.

Now let’s take a look at a more complex regexp that allows us to match a date in the YYYY-MM-DD format in a string:

The above pattern will enable us to check for a date in the YYYY-MM-DD format. However, it excludes invalid days of the month like: 2019–02–29 (since 2019 isn’t a leap year) or 2018–06–31 since June doesn’t have 31 days! You’ll need to perform specific validations for edge cases with additional code in your program.

Real-life use cases for regexps

The previous examples have showcased the most basic patterns of regular expressions. However, regexps have many other use cases in software development, for example:

Password patterns: When registering for most social media websites, you might have seen a message that asks you to create a password that has at least eight characters, one uppercase letter, one lowercase letter, one number, and one symbol.

The account creation form handles that your password meets these requirements using a regexp! The regexp syntax for the above example is:

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$

Looks cryptic or a little bit complicated 🤯?

E-mail patterns: How often have you seen an “Invalid e-mail address” message on an account creation form? Just like the password pattern, account creation forms handle the e-mail address validation using a regexp:

^[a-zA-Z0–9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0–9](?:[a-zA-Z0–9-]{0,61}[a-zA-Z0–9])?(?:\.[a-zA-Z0–9](?:[a-zA-Z0–9-]{0,61}[a-zA-Z0–9])?)*$

Even more complicated than the password pattern, right 🙃!?
The good news is that in modern websites created with HTML5, the <input type=”email”> tag automatically validates any entered e-mail address using the above regexp.

However, the above regexp might come in handy if you ever need to perform a back-end validation of an e-mail address in your application!

Credit card patterns: Another common use case for regexps is to validate credit card numbers. Of course, each credit card company has its format; so you would need to use different regexps to validate each one:

Visa:

^4[0–9]{12}(?:[0–9]{3})?$

MasterCard:

^(?:5[1–5][0–9]{2}|222[1–9]|22[3–9][0–9]|2[3–6][0–9]{2}|27[01][0–9]|2720)[0–9]{12}$

American Express:

^3[47][0–9]{13}$

There are other use cases for regexps, like time and phone number format validation. If you’re curious, you can look at 20 of the most common regular expressions software developers use.

How to get better at regexps?

Even though regexps might seem complex, they aren’t too challenging to learn. Your best bet to start learning them is writing regexps for basic patterns and then gradually moving on to more complex patterns.

Below you can take a look at a cheat sheet with the most basic regexp tokens:

And if you want to try and test your regular expressions without creating a Go program, you can use regexr! It is an excellent website that allows you to test and debug regexps.

An important detail is that it is unnecessary to reinvent the wheel for more complex regexp patterns. Remember that Google is your best friend searching for a specific regexp template! So before you go crazy trying to write a complex regexp, try googling for an existing or similar pattern first 🤪!

--

--

Hermann Rösch

AI Consultant and Software Engineer for fun 🧑🏻‍💻