英文:
How to use regexp2 to match []byte data?
问题
为了使代码返回true,你需要对正则表达式进行修改。在给定的代码中,正则表达式是(?s)^\x1c[\x01-\x0f]..............................................$
。这个正则表达式的含义是匹配以\x1c
开头,后面跟着一个范围在\x01
到\x0f
之间的字符,然后是任意长度的字符。但是,根据你提供的数据,data1
的开头是\x1c\x04\x04\xe9
,不符合正则表达式的要求。
要使代码返回true,你可以修改正则表达式,使其匹配data1
的开头。例如,你可以将正则表达式修改为(?s)^\x1c\x04\x04\xe9..............................................$
,这样就可以匹配data1
了。
修改后的代码如下:
package main
import (
"encoding/hex"
"fmt"
"github.com/dlclark/regexp2"
)
func MatchPattern(pattern string, data []byte) bool {
responseStr := string(data)
patternCompiled, err := regexp2.Compile(pattern, 0)
if err != nil {
fmt.Println("Compile fail")
return false
}
var foundItems []string
if x, _ := patternCompiled.FindStringMatch(responseStr); x != nil {
groups := x.Groups()
for i := range groups {
foundItems = append(foundItems, groups[i].String())
}
}
if len(foundItems) > 0 {
return true
}
return false
}
func main() {
data1, _ := hex.DecodeString("1c0404e900000000000a9cad14bd4f48e7de7d3f84a8df87c54f234b71b152f3e7df4bcda8c1292ae7df4bcda8c183c3")
data2, _ := hex.DecodeString("1c0404e900000000000a10ea14bd4f48e7df3582959b3b5ac54f234b71b152f3e7df4bcf71827088e7df4bcf7182b6ff")
pattern := "(?s)^\\x1c\\x04\\x04\\xe9..............................................$"
fmt.Println(MatchPattern(pattern, data1)) // true
fmt.Println(MatchPattern(pattern, data2)) // true
}
请注意,我只修改了正则表达式部分,将\x1c[\x01-\x0f]
修改为\x1c\x04\x04\xe9
以匹配data1
的开头。
英文:
Why did it fail the first time but succeed the second time?
package main
import (
"encoding/hex"
"fmt"
"github.com/dlclark/regexp2"
)
func MatchPattern(patern string, data []byte) (matched bool) {
responseStr := string(data)
patternCompiled, err := regexp2.Compile(patern, 0)
if err!=nil{
fmt.Println("Compile fail")
}
var foundItems []string
if x, _ := patternCompiled.FindStringMatch(responseStr); x != nil {
groups := x.Groups()
for i, _ := range groups {
foundItems = append(foundItems, groups[i].String())
}
}
//foundItems := m.PatternCompiled.FindStringSubmatch(responseStr)
if len(foundItems) > 0 {
matched = true
return
}
return false
}
func main() {
data1, _ := hex.DecodeString("1c0404e900000000000a9cad14bd4f48e7de7d3f84a8df87c54f234b71b152f3e7df4bcda8c1292ae7df4bcda8c183c3")
data2, _ := hex.DecodeString("1c0404e900000000000a10ea14bd4f48e7df3582959b3b5ac54f234b71b152f3e7df4bcf71827088e7df4bcf7182b6ff")
patern := "(?s)^\x1c[\x01-\x0f]..............................................$"
fmt.Println(MatchPattern(patern, data1)) // false
fmt.Println(MatchPattern(patern, data2)) // true
}
I want to use regexp2 to match []byte data. but as you see in the code, it returns false for data1. what can I do to make it return true?
答案1
得分: 2
问题出在你的模式上。请按以下方式更新它:
pattern := "(?s)^\x1c(?:[\x01-\x0f]..){2}...........................................$"
这个模式期望在\x01到\x0f范围内的字符重复两次,然后跟随任意两个字符。现在,data1和data2都应该返回true。
有关Go正则表达式的更多详细信息,请参考:https://golang.org/pkg/regexp/
英文:
The issue is with your pattern. Update it as follows:
patern := "(?s)^\x1c(?:[\x01-\x0f]..){2}...........................................$"
This pattern expects two repetitions of a character in the range \x01 to \x0f followed by any two characters. Now both data1 and data2 should return true.
For more details on Go regex, refer to: https://golang.org/pkg/regexp/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论