Golang Regexp Quote submatch [英] Golang Regexp Quote submatch
问题描述
我试图从正则表达式中提取一个子匹配值,但是如果有必要的话,它会忽略一组引号。到目前为止,我有这样的:
url: http: //play.golang.org/p/lcKLKmi1El
包主
导入fmt
importregexp
func main(){
authRegexp:= regexp.MustCompile(^ token =(?: \(。*) (*)。$)
$ b $匹配: )
matches = authRegexp.FindStringSubmatch(token = \llll \)
fmt.Println(MATCHES,matches,len(matches))
}
输入
:: 预期匹配
令牌= llll
:: [token = llll llll]
token =llll
:: [token =llllllll]
另外请注意,我想测试无引号或单个集合的报价。我不希望能够有不匹配的引号或任何东西。
我如何摆脱空翻的字符串?有没有更好的正则表达式来摆脱引号?
谢谢
好吧,就是这样: http://play.golang.org/p/h2w -9-XFAt
正则表达式: I am trying to extract a submatch value from a regexp but to all for it to disregard a set of quotes if necessary. So far I have this: url: http://play.golang.org/p/lcKLKmi1El Also note that I want to test for either no quotes, or a single set of quotes. I dont want to be able to have mismatched quotes or anything. How do I get rid of the empty string that is rerturned? Is there a better regex to get rid of the quotes? Thanks Ok, that's it: http://play.golang.org/p/h2w-9-XFAt Regex:
这篇关于Golang Regexp Quote submatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ^ token =?([^] *)?$ $ c
$ $ p $ MATCHES [token = llll llll] 2
MATCHES [token =llllllll] 2
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]
^token="?([^"]*)"?$
MATCHES [token=llll llll] 2
MATCHES [token="llll" llll] 2