[英]Golang Regexp Quote submatch
我試圖從正則表達式中提取一個子匹配值,但是如果需要的話,它會忽略一組引號。 到目前為止我有這個:
網址: http : //play.golang.org/p/lcKLKmi1El
package main
import "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 Matches
token=llll
:: [token=llll llll]
token="llll"
:: [token="llll" llll]
另請注意,我想測試沒有引號或一組引號。 我不希望能有不匹配的引號或任何東西。
如何擺脫返回的空字符串? 是否有更好的正則表達式來擺脫報價?
好的,就是這樣: http : //play.golang.org/p/h2w-9-XFAt
正則表達式: ^token="?([^"]*)"?$
MATCHES [token=llll llll] 2
MATCHES [token="llll" llll] 2
請嘗試以下方法:
authRegexp := regexp.MustCompile("^token=(.*?|\\".*?\\")$")
在這里演示 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.