doushou8730
2019-04-02 03:43 阅读 34
已采纳

golang regex键:值问题

I'm trying to split a syslog message into a key:value 'map[string]string', but cannot seem to get the regex right.

The source string delimits its keys and values by an '='. However, each pair is seperated by spaces with spaces actually being within the values. I have it pretty close but as you can see from the link below, I can't get the rest of the 'start', 'end', or 'suser' values.

Here's a link to a golang playground showing my exact problem:

package main

import (
    "fmt"
    "regexp"
)

var rex = regexp.MustCompile("(\\w+)=(.+?[^\\\\s\\\\w+=]+)")

func main() {
    conn := "start=Mar 26 2019 10:36:14 +00:00 end=Mar 26 2019 10:36:14 +00:00 suser=AD.COM\\\\Username src=10.1.0.51"
    data := rex.FindAllStringSubmatch(conn, -1)

    res := make(map[string]string)
    for _, kv := range data {
        k := kv[1]
        v := kv[2]
        res[k] = v
    }
    for v := range res {fmt.Printf("%s: %v
", v, res[v])}

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duandou2763 duandou2763 2019-04-02 07:05

    You may use

     var rex = regexp.MustCompile(`(\w+)=([^=]*\w)(?:\s|$)`)
    

    See the Go demo, the regex demo online and the Regulex graph.

    enter image description here

    Regex details

    • (\w+) - Capturing group 1: one or more word chars
    • = - a = char
    • ([^=]*\w) - Capturing group 2: any 0+ chars other than = and then a word char
    • (?:\s|$) - either a whitespace or end of string.
    点赞 评论 复制链接分享
  • duanluo5096 duanluo5096 2019-04-02 05:07

    Maybe try the following pattern:

    (\\s*[^\\s]+)=([^=]*[\\w\\d]+\\s|[\\d.]+)
    

    Result:

    start: Mar 26 2019 10:36:14 +00:00 
    end: Mar 26 2019 10:36:14 +00:00 
    suser: AD.COM\\Username 
    src: 10.1.0.51
    

    Example:

    https://play.golang.org/p/R8rE_GGZF_W

    点赞 评论 复制链接分享

相关推荐