Finding the substring in text by the regex pattern
There are always tasks such as validating the input, searching the document for any information, or even cleaning up a given string from unwanted escape characters. For these cases, regular expressions are usually used.
The Go standard library contains the regexp
package, which covers the operations with regular expressions.
How to do it...
- Open the console and create the folder
chapter02/recipe07
. - Navigate to the directory.
- Create the
regexp.go
file with the following content:
package main import ( "fmt" "regexp" ) const refString = `[{ \"email\": \"[email protected]\" \ "phone\": 555467890}, { \"email\": \"[email protected]\" \ "phone\": 555467890}]` func main() { // This pattern is simplified for brevity emailRegexp := regexp.MustCompile("[a-zA-Z0-9]{1,} ...