2

I need to get string between two delimters. And these pair of delimters are different. Here is my text:

[2018.07.10 00:30:03:640][TraceID: 8HRWSI105YVO91]->StartExecuteTask\35
[2018.07.10 00:30:18:585][TraceID: 8707HFH7NR6307]->RequestInfo\27
[2018.07.10 00:30:18:585][TraceID: 8707HFH7NR6307]->RequestExecuteEnd\16
[2018.07.10 00:30:18:585][TraceID: 8707HFH7NR6307]->OutgoingData\26651
[2018.07.10 00:31:16:773][TraceID: G8EM5LANBPC32H]->CheckUserInfo\141

I need to get time, traceid and requset type (after -> before \{d} )

And here is my regexp:

[\[|\->](.*?)[\\|\]]

What i get:

2018.07.10 00:30:03:640   TraceID: 8HRWSI105YVO91  >StartExecuteTask
2018.07.10 00:30:18:585   TraceID: 8707HFH7NR6307  >RequestInfo

I can't remove the > from last match. Or maybe whole regexp is wrong?

Digital God
  • 413
  • 1
  • 5
  • 16

1 Answers1

3

You may use

(?:\[|->)(.*?)[\\\]]

See the regex demo

In Go, declare as

var re = regexp.MustCompile(`(?:\[|->)(.*?)[\\\]]`)

Details

  • (?:\[|->) - a non-capturing group that matches either [ char or -> substring
  • (.*?) - Group 1: any 0+ chars, other than line break chars, as few as possible
  • [\\\]] - a \ or ] char.

Demo:

package main

import (
    "regexp"
    "fmt"
)

func main() {
    var re = regexp.MustCompile(`(?:\[|->)(.*?)[\\\]]`)
    var str = `[2018.07.10 00:30:03:640][TraceID: 8HRWSI105YVO91]->StartExecuteTask\35`

    for _, match := range re.FindAllStringSubmatch(str, -1) {
        fmt.Println(match[1])
    }   
}

Output:

2018.07.10 00:30:03:640
TraceID: 8HRWSI105YVO91
StartExecuteTask
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397