如何使用regexp2来匹配[]byte数据?

huangapple go评论110阅读模式
英文:

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/

huangapple
  • 本文由 发表于 2023年4月13日 16:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定