I am trying to capture lines that look like this, for example:
2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process'
I am using the following regex pattern:
\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} \[.*\]\[.*\] Executing Process '.*'
However, the line is not being captured. I noticed that it works all the way up to the single quote. But if I add the single quote, it fails. I thought there might possibly be two different types of single quotes, but I copied and pasted the quote being used, and it still didn't work. Also both quotes are being generated by the same code, so one single quote should be the same as another.
EDIT:
This the regex code.
regexPattern := `\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} \[.*\]\[.*\] Executing Process '.*'`
log = highlight(log, regexPattern)
...
func highlight(log, pattern string) string {
regex := regexp.MustCompile(
matches := regex.FindAllString(log, -1)
Edit:
I figured it out. The log that I was getting back was HTML escaped, so the single quote was actually '. That prevented matching with the single quote
Please try this slightly modified regex.
(\d){4}\/(\d){2}\/(\d){2} (\d){2}:(\d){2}:(\d){2} (\[\w*\]){2} ([\w ])+ '.*'
I hope it helps! If you have questions, please ask!
Try this working sample code (with '
):
package main
import "fmt"
import "regexp"
import "time"
var rgx = regexp.MustCompile(`(\d){4}\/(\d){2}\/(\d){2} (\d){2}:(\d){2}:(\d){2} (\[\w*\]){2} ([\w ])+ '.*'`)
func main() {
s := time.Now().UTC().Format("2006/01/02 15:04:05") + ` [DEV][INFO] Executing Process 'Some process' `
r := rgx.FindAllString(s, -1)
fmt.Println(r)
}
output:
[2016/08/11 18:13:41 [DEV][INFO] Executing Process 'Some process']
Try this working sample code (with '
):
package main
import "fmt"
import "regexp"
import "time"
var rgx = regexp.MustCompile(`(\d){4}\/(\d){2}\/(\d){2} (\d){2}:(\d){2}:(\d){2} (\[\w*\]){2} ([\w ])+ '.*'`)
func main() {
s := time.Now().UTC().Format("2006/01/02 15:04:05") + ` [DEV][INFO] Executing Process 'Some process' `
r := rgx.FindAllString(s, -1)
fmt.Println(r) //
s = `2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process'`
r = rgx.FindAllString(s, -1)
fmt.Println(r) // 2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process'
fmt.Println()
s = ` dfgfsdfg sdf gsdf gsdf 2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process' 2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process'`
r = rgx.FindAllString(s, -1)
fmt.Println(r) // [2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process' 2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process']
fmt.Println()
s = `aaaaaaaaaaaaaaaaa asdfsf 'asdfasdf'`
r = rgx.FindAllString(s, -1)
fmt.Println(r) // []
}
output;
[2016/08/11 18:09:01 [DEV][INFO] Executing Process 'Some process']
[2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process']
[2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process' 2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process']
[]