Skip to content

Instantly share code, notes, and snippets.

@mariusk
Created December 19, 2012 16:46
Show Gist options
  • Save mariusk/4338168 to your computer and use it in GitHub Desktop.
Save mariusk/4338168 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"regexp"
)
var str string = `cpu 143074 414 36348 4569263 926 3 732 0 0 0
cpu0 54787 108 14071 1114566 490 1 285 0 0 0
cpu1 19898 16 4065 1165614 54 0 39 0 0 0
cpu2 52089 234 14503 1117793 276 1 390 0 0 0
cpu3 16298 56 3708 1171288 105 0 16 0 0 0`
func main() {
r, _ := regexp.Compile(`(?m)^(cpu\d+) (\d+)`)
m := r.FindAllStringSubmatch(string(str), -1)
fmt.Printf("%v\n", m)
for k, v := range m {
fmt.Printf("%d: %#v\n", k, v)
}
}
/* outputs:
[cpu0 54787 cpu1 19898 cpu2 52089 cpu3 16298]
0: "cpu0 54787"
1: "cpu1 19898"
2: "cpu2 52089"
3: "cpu3 16298"
*/
/* want something like:
0: [["cpu0"] ["54787"]]
1: [["cpu1"] ["19898"]]
2: [["cpu2"] ["52089"]]
3: [["cpu3"] ["16298"]]
*/
/* Found solution which is close enough:
0: []string{"cpu0 73674", "cpu0", "73674"}
1: []string{"cpu1 32192", "cpu1", "32192"}
2: []string{"cpu2 68983", "cpu2", "68983"}
3: []string{"cpu3 27855", "cpu3", "27855"}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment