Regular Expressions in Go

Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
Search for a command to run...

Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
No comments yet. Be the first to comment.
Goroutines are one of the best features of Go language. It is very easy to run a function as a goroutine, you just have to use the keyword go before the function call. Here is a very simple function which sleeps for n seconds and prints a line. pa...

In a previous post, we saw how we can use Regular expressions in Go to match and replace patterns in a string. While regexps are very useful, you might not want to them for every time you want to do some basic string manipulation. The strings package...

Go is a modern language, but doesn't use try-except blocks to handle errors. Errors are simple values that can be returned from functions and it is common to check for errors before proceeding with your next steps. Let's see the different ways to cre...

In the previous post, we saw how to write a simple unit test case and also how to write table driven test cases. In this post we will quickly see how to write benchmarks, as go supports running benchmarks on your functions as part of the standard tes...
Go has built-in support for writing test cases for your code. And it is important to write as much test cases as possible to make sure you cover all possible conditions. You have to use the testing standard package to write your unit test cases. Let...
Pro Golang Dev
14 posts
Regular Expressions is one skill that is essential to any programmer. We deal with lot of unstructured data and regexps are one vital tool to parse and process it.

Lets look at the different functions and methods available to construct and use regular expressions in Go. For this tutorial I am going to assume you know the basics of Regex.
Golang has the regexp package, which makes matching regular expressions on strings easy. To do a basic substring match, we can use the regexp.MatchString function.
matched, err := regexp.MatchString(`fo.`, "a quick brown fox jumped over the lazy dog")
fmt.Println(matched) // true
fmt.Println(err) // nil (regexp is valid)
This regex matches the substring "fox" in the string. The variable matched is a boolean value depending on whether a match was found or not. You can use all the usual regex patterns like ^ and $ to match beginning/ending words.
If you are using the same pattern for matching in multiple places in your code, it makes sense to compile the pattern into a Regexp object. Use the regexp.Compile function to compile your pattern. If the pattern isn't a well-formed regular expression, the err variable will show it. If you use the regexp.MustCompile function, the program panics on an error in the expression.
While compiling Regexps, it makes sense to use raw strings so you don't have to escape your double quotes. But the other special characters need to be escaped with a backslash.
re := regexp.MustCompile(`ba.?`)
fmt.Printf("%q\n", re.FindString("foo bar baz")) // "bar"
fmt.Printf("%q\n", re.FindString("spam eggs")) // ""
Once a Regexp object is created, you can use the FindString method to match the patterns. It returns the first matching string if there is a successful match, else it returns an empty string.
If you want to find the starting and ending of the match, you can use the FindStringIndex method. It returns a slice of integers to show the matching positions.
re := regexp.MustCompile(`ab?`)
fmt.Println(re.FindStringIndex("table")) // [1 3]
fmt.Println(re.FindStringIndex("foo") == nil) // true
And to find all the matches, you can use the FindAllString method, which returns a slice of strings as the output. If there are no matches, you get back an empty slice. The method takes an integer argument n; if n >= 0, the function returns at most n matches. If you pass -1, it returns all matches.
re := regexp.MustCompile(`fo.`)
fmt.Printf("%q\n", re.FindAllString("food fox foobar", -1)) // ["foo" "fox" "foo"]
fmt.Printf("%q\n", re.FindAllString("food fox foobar", 2)) // ["foo" "fox"]
fmt.Printf("%q\n", re.FindAllString("spam eggs", -1)) // []
Now that you know how to find strings based on a pattern, you would like to know how to replace strings. You can use the ReplaceAllString method to replace the text of all matches. It returns a new string, with all the matches of the regexp replaced with a replacement string. You can use the $ symbols in the replacement string to show you need to expand the matched group.
re := regexp.MustCompile("a(x*)b")
fmt.Println(re.ReplaceAllString("-ab-axxb-", "T")) // -T-T-
fmt.Println(re.ReplaceAllString("-ab-axxb-", "$1")) // --xx-
In this example, we have a pattern, which also has a group (x*) which matches 0 or more x characters. In the first replacement example, we are replace both ab and axxb with the letter T - simple enough.
In the second example we use $1 to replace with the matched first group (in this case xx). That is why we get --xx- as the output.
Another common function that is used a lot when you use regular expressions is the Split function. It splits a string into multiple slices of strings based on the pattern it matches. Just like FindAllString method, this also takes an integer n; if n >= 0, the method returns at most n strings. If you pass -1, it returns all split strings.
a := regexp.MustCompile(`a`)
fmt.Printf("%q\n", a.Split("banana", -1)) // ["b" "n" "n" ""]
fmt.Printf("%q\n", a.Split("banana", 0)) // [] (nil slice)
fmt.Printf("%q\n", a.Split("banana", 1)) // ["banana"]
fmt.Printf("%q\n", a.Split("banana", 2)) // ["b" "nana"]
zp := regexp.MustCompile(`z+`)
fmt.Printf("%q\n", zp.Split("pizza", -1)) // ["pi" "a"]
fmt.Printf("%q\n", zp.Split("pizza", 0)) // [] (nil slice)
fmt.Printf("%q\n", zp.Split("pizza", 1)) // ["pizza"]
fmt.Printf("%q\n", zp.Split("pizza", 2)) // ["pi" "a"]
These are the most commonly used functions and methods related to Regular expressions in Go. What other functions do you use? Can you give any more examples?
Pro Tip: Use the site RegexPal to construct and debug your pattern