mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-06 20:30:12 +02:00
learning about regexp capture groups, seems like it could be quite useful...
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package learning
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/cast"
|
||||
)
|
||||
|
||||
func CaptureBingoBoard(board string) [][]int {
|
||||
var nums [][]int
|
||||
// parens create indexed capture groups
|
||||
// when used with (*regexp).FindStringSubmatch a string slice is returned
|
||||
pattern := regexp.MustCompile(`\s?(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)`)
|
||||
|
||||
for _, row := range strings.Split(board, "\n") {
|
||||
matches := pattern.FindStringSubmatch(row)
|
||||
if matches == nil {
|
||||
panic("row does not match pattern: " + row)
|
||||
}
|
||||
|
||||
// submatch[0] is the entire string
|
||||
// submatch[1:] are the captured groups that i'm interested in
|
||||
var rowNums []int
|
||||
for _, v := range matches[1:] {
|
||||
rowNums = append(rowNums, cast.ToInt(v))
|
||||
}
|
||||
nums = append(nums, rowNums)
|
||||
}
|
||||
|
||||
return nums
|
||||
}
|
||||
Reference in New Issue
Block a user