正则表达式

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. )
  7. var pattern = `^([\*]+)\n`
  8. var negate = false
  9. var content = `******************************************************************
  10. 【记录时间】:2021-05-13 10:06:16
  11. 【记录机器】:JY-weishangchen
  12. 【请求IP】:10.6.151.100
  13. 【请求地址】:https://w-wines-olapp.cofco.com/KmAPI/Api/Open/VipInfo/IsExistVip
  14. 【请求身份】:FSJLB001
  15. 【请求接口】:VipInfo/IsExistVip
  16. 【请求商户】:55333
  17. 【请求时间】:2021-05-13 10:06:16 099
  18. 【请求Key】:xcx_mzh_shop_xxx
  19. 【请求头】:{"CustId":55333,"Sign":"610d29ce1e510ef242dd9cd73478001f","Timestamp":1622167577,"Appid":"FSJLB001","Nonce":"xcx_mzh_shop_xxx","Version":"1.0"}
  20. 【请求参数】:{"CustID":"55333","UnionId":"o9odR5hVeSzbRCjjC6aeLwMNpqFk"}
  21. 【响应时间】:2021-05-13 10:06:16 130
  22. 【响应内容】:{"data":false,"extInfo":null,"code":"0002","msg":"当前会员未注册","errmsg":"","IsOk":false}
  23. 【执行耗时】:31
  24. ******************************************************************
  25. `
  26. func main() {
  27. regex, err := regexp.Compile(pattern)
  28. if err != nil {
  29. fmt.Println("Failed to compile pattern:", err)
  30. return
  31. }
  32. matches := regex.MatchString(content)
  33. if negate {
  34. matches = !matches
  35. }
  36. fmt.Printf("%v\t%v\n", matches, content)
  37. lines := strings.Split(content, "\n")
  38. fmt.Printf("matches\t line\n")
  39. for _, line := range lines {
  40. matches := regex.MatchString(line)
  41. if negate {
  42. matches = !matches
  43. }
  44. fmt.Printf("%v\t%v\n", matches, line)
  45. }
  46. }

参考链接

https://play.golang.org/ https://www.elastic.co/guide/en/beats/filebeat/7.13/multiline-examples.html