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

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测