简体   繁体   English

Go lang从字符串中获取匹配的子字符串

[英]Go lang get matching substring from string

I'm trying to extract all words from a string which are between quotes. 我试图从引号之间的字符串中提取所有单词。

Here's my current code: 这是我当前的代码:

func StrExtract(word string) []string {
  r, _ := regexp.Compile(`".*"`)
  result := r.FindAllString(word, -1)
  RemoveDuplicates(&result)
  return (result)
}

Test the code here 这里测试代码

With an input like: 输入如下:

`Hi guys, this is a "test" and a "demo" ok?`

I get the output: 我得到的输出:

["test" and a "demo"]

But I'd like to get: 但我想得到:

[test demo]

Please help me fix this, or suggest better alternatives. 请帮助我解决此问题,或提出更好的替代方案。

You can just add a lazy quantifier .*? 您可以只添加一个懒惰的量词.*? , ".*?" ".*?" being the regex, if you want to keep it simple. 作为正则表达式,如果您想保持简单。 The reason you are getting "test" and a "demo" is because just .* is greedy and matches as much text as possible (therefore, it actually matches the " before test and after demo , ignoring the fact that there are other quotes in between). 你得到的理由"test" and a "demo"是因为刚.*是贪婪和尽可能多的文本可能的(相匹配,因此,它实际上是匹配的"test和之后demo ,忽略了一个事实,有其他报价之间)。

Normally a better but in some ways slightly more complicated way to do this is using character classes "[^"]*" , disabling matching quotes in between. This can also cause some other behaviors like including newlines (in which case you can also disable them [^"\\n] , or perhaps you actually want such a case) 通常,执行此操作的一种更好但更复杂的方法是使用字符类"[^"]*" ,禁用两者之间的匹配引号。这还会导致其他行为,例如包括换行符(在这种情况下,您也可以禁用他们[^"\\n] ,或者您实际上想要这样的情况)

Since you want to also not have the quotes some additional things need to be done. 由于您也不想使用引号,因此需要做一些其他事情。 You can do that with either lookarounds: (?<=")[^"]*(?=") , or with capture groups: "(.*?)" and "([^"]*)" . 您可以通过以下两种方法来做到这一点: (?<=")[^"]*(?=")或捕获组: "(.*?)""([^"]*)" If you choose the capture group route, you have to use the capture group, not whole matches. 如果选择捕获组路由,则必须使用捕获组,而不是全部匹配。

Regex: 正则表达式:

"(.*?)"

Here is an online demo: https://regex101.com/r/sI4tA9/1 这是一个在线演示: https : //regex101.com/r/sI4tA9/1

All you have to do now is to join matches. 您现在要做的就是加入比赛。 Unfortunately I'm not so into go that's why I can't help you in that case 不幸的是我没有那么到go ,这就是为什么我不能帮你在这种情况下,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM