Golang Regexp Quote submatch [英] Golang Regexp Quote submatch

查看:99
本文介绍了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



正则表达式: ^ token =?([^] *)?$
$ $ p $ MATCHES [token = llll llll] 2
MATCHES [token =llllllll] 2


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

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]

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: ^token="?([^"]*)"?$

MATCHES [token=llll llll] 2
MATCHES [token="llll" llll] 2

这篇关于Golang Regexp Quote submatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆