我正在尝试捕获看起来像这样的行,例如:
2016/07/27 21:37:50 [DEV][INFO] Executing Process 'Some process'
我使用以下正则表达式模式:
\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} \[.*\]\[.*\] Executing Process '.*'
然而,该线未被捕获。我注意到它一直工作到单引号。但如果我添加单引号,它就会失败。我认为可能有两种不同类型的单引号,但我复制并粘贴了所使用的引用,但它仍然没有用。这两个引号也是由相同的代码生成的,因此一个引号应该与另一个引用相同。
编辑:
这是正则表达式代码。
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)
编辑:
我想到了。我回来的日志是HTML转义的,所以单引号实际上是#39。这阻止了与单引号的匹配
答案 0 :(得分:0)
尝试使用此工作示例代码(使用'
):
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)
}
输出:
[2016/08/11 18:13:41 [DEV][INFO] Executing Process 'Some process']
尝试使用此工作示例代码(使用'
):
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) // []
}
输出;
[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']
[]
答案 1 :(得分:-1)
请尝试这个稍加修改的正则表达式。
(\d){4}\/(\d){2}\/(\d){2} (\d){2}:(\d){2}:(\d){2} (\[\w*\]){2} ([\w ])+ '.*'
我希望它有所帮助!如果您有任何疑问,请询问!