dongxinm279890
2015-07-24 10:03
浏览 274
已采纳

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

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqenv99518 2015-07-24 10:06
    已采纳

    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).

    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 [^" ], or perhaps you actually want such a case)

    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.

    点赞 评论
  • doo6568 2015-07-24 10:39

    Regex:

    "(.*?)"

    Here is an online demo: 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

    点赞 评论

相关推荐 更多相似问题