Advanced Regex in go language with examples.
Table of contents
Regular expressions, or regex, are powerful tools used to match patterns in text. In the Go language, regex is supported by the "regexp" package. In this article, we will explore advanced regex concepts in Go, including lookaround, backreferences, and capturing groups.
Lookaround
Lookaround is a technique used to match patterns based on what comes before or after them, without including the matched text in the final output. There are two types of lookaround in regex: lookahead and lookbehind.
Lookahead matches a pattern only if it is followed by another pattern. For example, let's say we have a string of numbers separated by commas:
s := "1,2,3,4,5"
We want to match all numbers that are followed by a comma. We can use lookahead to accomplish this:
re := regexp.MustCompile(`\d+(?=,)`)
matches := re.FindAllString(s, -1)
fmt.Println(matches) // Output: [1 2 3 4]
The (?=,)
part of the regex is the lookahead. It matches any digits that are followed by a comma.
Lookbehind
Lookbehind matches a pattern only if it is preceded by another pattern. For example, let's say we have a string of numbers separated by hyphens:
s := "1-2-3-4-5"
We want to match all numbers that are preceded by a hyphen. We can use lookbehind to accomplish this:
re := regexp.MustCompile(`(?<=-)\d+`)
matches := re.FindAllString(s, -1)
fmt.Println(matches) // Output: [2 3 4 5]
The (?<=-)
part of the regex is the lookbehind. It matches any digits that are preceded by a hyphen.
Backreferences
Backreferences are used to match patterns that have already been matched earlier in the string. They are denoted by the backslash character followed by a number, such as \1
or \2
.
For example, let's say we have a string of HTML tags:
s := "<h1>Title</h1>"
We want to match the closing tag that corresponds to the opening tag. We can use backreferences to accomplish this:
re := regexp.MustCompile(`<(\w+)>.*</\1>`)
match := re.FindString(s)
fmt.Println(match) // Output: <h1>Title</h1>
The (\w+)
part of the regex matches the opening tag, and the </\1>
part matches the closing tag that corresponds to the opening tag.
Capturing Groups
Capturing groups are used to extract parts of a matched pattern. They are denoted by parentheses, such as ()
.
For example, let's say we have a string of phone numbers in the format "XXX-XXX-XXXX":
s := "123-456-7890"
We want to extract the area code (the first three digits). We can use a capturing group to accomplish this:
re := regexp.MustCompile(`(\d{3})-\d{3}-\d{4}`)
match := re.FindStringSubmatch(s)
fmt.Println(match[1]) // Output: 123
The (\d{3})
part of the regex is the capturing group that matches the area code. The FindStringSubmatch
method returns an array of all the matched substrings, including the capturing group.
Conclusion
In this article, we explored advanced regex concepts in Go language, including lookaround, lookbehinde, backreference, and group matching which will help you to find the mobile number, email matching, or certain occurrences in the string data.
I hope this helps, you!!
More such articles:
https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA
\==========================**=========================
If this article adds any value to you then please clap and comment.
Let’s connect on Stackoverflow, LinkedIn, & Twitter.