I have a superheroes string, all of them have names, but not all of them have attributes.
It has a format of ⛦name⛯attrName☾attrData☽, where the attrName☾attrData☽ is optional.
So, the superheroes string is:
⛦superman⛯shirt☾blue☽⛦joker⛯⛦spiderman⛯age☾15yo☽girlFriend☾Cindy☽
I want to use Regex to extract the string, and populates the result into a slice of map, as such:
[ {name: superman, shirt: blue},
{name: joker},
{name: spiderman, age: 15yo, girlFriend: Cindy} ]
I can't get it done in Go playground. I use the regex ⛦(\\w+)⛯(?:(\\w+)☾(\\w+)☽)*, but it only can capture single attribute, i.e. regex unable to capture the age attributes.
My code is:
func main() {
re := regexp.MustCompile("⛦(\\w+)⛯(?:(\\w+)☾(\\w+)☽)*")
fmt.Printf("%q\n", re.FindAllStringSubmatch("⛦superman⛯shirt☾blue☽⛦joker⛯⛦spiderman⛯age☾15yo☽girlFriend☾Cindy☽", -1))
}
The Go Playground code is at here: https://play.golang.org/p/Epv66LVwuRK
The run result is:
[
["⛦superman⛯shirt☾blue☽" "superman" "shirt" "blue"]
["⛦joker⛯" "joker" "" ""]
["⛦spiderman⛯age☾15yo☽girlFriend☾Cindy☽" "spiderman" "girlFriend" "Cindy"]
]
The age is missing, any idea?