我試圖從正則表達(dá)式中提取一個(gè)子匹配值,但如果需要,它可以忽略一組引號(hào)。到目前為止,我有這個(gè):網(wǎng)址:http : //play.golang.org/p/lcKLKmi1Elpackage mainimport "fmt"import "regexp"func main() { authRegexp := regexp.MustCompile("^token=(?:\"(.*)\"|(.*))$") matches := authRegexp.FindStringSubmatch("token=llll") fmt.Println("MATCHES", matches, len(matches)) matches = authRegexp.FindStringSubmatch("token=\"llll\"") fmt.Println("MATCHES", matches, len(matches))}Input::Expected Matchestoken=llll::[token=llll llll]token="llll"::[token="llll" llll]另請(qǐng)注意,我想測(cè)試沒有引號(hào)或一組引號(hào)。我不希望能夠有不匹配的引號(hào)或任何東西。如何擺脫返回的空字符串?有沒有更好的正則表達(dá)式來擺脫引號(hào)?
Golang 正則表達(dá)式引用子匹配
拉風(fēng)的咖菲貓
2021-10-25 17:13:34