Ad space (banner)
๐ŸนGo Lessons
Lesson 32 / 68

Intro to Regular Expressions (Pattern Matching)

In this lesson you will learn the basics of pattern matching with regular expressions in Go so you can check whether text follows a given format. This is for anyone searching for "Go regular expressions how to use".

A regular expression is a special notation that describes the shape of a string. You can specify a pattern such as "three digits, a hyphen, then four digits", check whether text matches it, and pull out the matching part. Go provides the regexp package.

The sample code compiles a phone-number-like pattern with regexp.MustCompile(...) and extracts the matching part with re.FindString(text). As re2.MatchString("12345") shows, the same pattern can also simply answer whether the text matches.

A common stumbling block for beginners is learning what all the symbols mean. It looks like nothing but punctuation at first, but it is a powerful tool that pays off in practice, for example when validating email formats.

In real-world development, learning regular expressions once gives you a skill that transfers to most other programming languages.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)