如何在Go中进行可选的正则表达式匹配

Say I have the following two types of string inputs:

1) "feat: this is a feature"
2) "feat[JIRA-xxxx]: this is Jira feature

The strings contains three parts, there feat is Type, JIRA-xxx inside [] is optional Jira story id, and rest part after : is subject.

I want to use a single regexp to match the three parts, optional file should get nil if not present. The following is my code:

var re *regexp.Regexp
re = regexp.MustCompile("^(\\w*)(\\[(.*)\\])?\\:\\s(.*)$")

input := "feat[JIRA-xxxx]: this is Jira feature"
res := re.FindAllStringSubmatch(input, -1)
fmt.Print("%q", res)

My regular expression has a problem. Because the Jira ID part is optional, I have to use ()?, but () has the other meaning of returning matched value, so with my code, it returns four matches:

`feat`, `[JIRA-xxx]`, JIRA-xxx`, `this is Jira feature`

How to modify the expression so that I get only the intended 3 matches?

Use the ?: modifier to create a non capturing group:

re = regexp.MustCompile("^(\\w*)(?:\\[(.*)\\])?\\:\\s(.*)$")

Run it on the playground.