Extracting data from strings
You can use a regular expression to locate and extract text that occurs within a pattern.
How to do it...
Use capture groups to extract substrings that match a pattern.
How it works...
package main import ( "fmt" "regexp" ) func main() { re := regexp.MustCompile(`^(\w+)=(\w+)$`) result := re.FindStringSubmatch(`property=12`) fmt.Printf("Key: %s value: %s\n", result[1], result[2]) result = re.FindStringSubmatch(`x=y`) fmt.Printf("Key: %s value: %s\n", result[1], result[2]) }
Here is the output:
Key: property value: 12 Key: x value: y
Let’s look at this regular expression:
^(\w+)
: A string composed of one or more word characters at the beginning of the line (capture group 1)
...