正则表达式用于处理数字签名的多个条目。

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

Regex to process multiple entries for a digital signature

问题

我在这里有正则表达式和相应的匹配输出:
https://regexr.com/6jiei

它目前可以匹配所有条目的IP和时间戳,但我还想要识别的十六进制值。

例如,对于以下输入字符串

IDVal 4273E6D162ED2717A1CF4207A254004CD3F5307B
Posted 2022-12-28 07:35:55
Status 2022-12-28 08:10:11
Entry 94.62.86.22 2022-12-28 11:10:30
Entry 21.12.26.23 2022-12-28 13:10:30
Entry 113.132.26.203 2022-12-28 12:56:30
Entry 31.12.27.22 2022-12-28 12:35:30
IDVal 4273E6D162ED2717A1CF4207A254004CD3F5307B
Posted 2022-12-28 07:35:55
Status 2022-12-28 08:10:11
Entry 94.62.86.22 2022-12-28 11:10:30
Entry 21.12.26.23 2022-12-28 13:10:30
Entry 113.132.26.203 2022-12-28 12:56:30
Entry 31.12.27.22 2022-12-28 12:35:30
IDVal 0D12D8E72DED99EE31BB0C57789352BED0CEEEFF
Posted 2022-12-28 07:30:55
Status 2022-12-28 06:10:11
Entry 51.102.52.36 2022-12-28 07:10:30

输出应该匹配/捕获:

4273E6D162ED2717A1CF4207A254004CD3F5307B
94.62.86.22 2022-12-28 11:10:30
21.12.26.23 2022-12-28 13:10:30
113.132.26.203 2022-12-28 12:56:30
31.12.27.22 2022-12-28 12:35:30
4273E6D162ED2717A1CF4207A254004CD3F5307B
94.62.86.22 2022-12-28 11:10:30
21.12.26.23 2022-12-28 13:10:30
113.132.26.203 2022-12-28 12:56:30
31.12.27.22 2022-12-28 12:35:30
0D12D8E72DED99EE31BB0C57789352BED0CEEEFF
51.102.52.36 2022-12-28 07:10:30

我尝试了这个正则表达式:

(?s)(\b[A-F\d]{40}\b).*?(\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b).?(\b\d{4}(-\d{2}){2} (\d{2}:){2}\d{2}\b)

它可以返回十六进制数字签名,但只返回第一个Entry,而不返回所有对应的条目。

英文:

I have the regex and corresponding match output here :
https://regexr.com/6jiei

Which matches IP and time stamp for all entries currently but I also want the hexadecimal value for identification.

For ex for below Input string:

IDVal 4273E6D162ED2717A1CF4207A254004CD3F5307B
Posted 2022-12-28 07:35:55
Status 2022-12-28 08:10:11
Entry 94.62.86.22 2022-12-28 11:10:30
Entry 21.12.26.23 2022-12-28 13:10:30
Entry 113.132.26.203 2022-12-28 12:56:30
Entry 31.12.27.22 2022-12-28 12:35:30
IDVal 4273E6D162ED2717A1CF4207A254004CD3F5307B
Posted 2022-12-28 07:35:55
Status 2022-12-28 08:10:11
Entry 94.62.86.22 2022-12-28 11:10:30
Entry 21.12.26.23 2022-12-28 13:10:30
Entry 113.132.26.203 2022-12-28 12:56:30
Entry 31.12.27.22 2022-12-28 12:35:30
IDVal 0D12D8E72DED99EE31BB0C57789352BED0CEEEFF
Posted 2022-12-28 07:30:55
Status 2022-12-28 06:10:11
Entry 51.102.52.36 2022-12-28 07:10:30

Output should match/capture :

4273E6D162ED2717A1CF4207A254004CD3F5307B
94.62.86.22 2022-12-28 11:10:30
21.12.26.23 2022-12-28 13:10:30
113.132.26.203 2022-12-28 12:56:30
31.12.27.22 2022-12-28 12:35:30
4273E6D162ED2717A1CF4207A254004CD3F5307B
94.62.86.22 2022-12-28 11:10:30
21.12.26.23 2022-12-28 13:10:30
113.132.26.203 2022-12-28 12:56:30
31.12.27.22 2022-12-28 12:35:30
0D12D8E72DED99EE31BB0C57789352BED0CEEEFF
51.102.52.36 2022-12-28 07:10:30

I tried this regex :

(?s)(\b[A-F\d]{40}\b).*?(\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b).?(\b\d{4}(-\d{2}){2} (\d{2}:){2}\d{2}\b)

It then returns the hexadecimal digital signature, but then it only returns the first Entry and doesn't returns all the corresponding entries.

答案1

得分: 2

你可以从后面开始构建查询。

案例14273E6D162ED2717A1CF4207A254004CD3F5307B

  • 根据你的正则表达式尝试,它是一个长度为40的多个字母数字字符:[\w]{40}

案例2113.132.26.203 2022-12-28 12:56:30

  • 一组数字和冒号:[\d:]+
  • 一个空格
  • 一组数字和短横线:[\d-]+
  • 一个空格
  • 一组数字和句点:[\d\.]+

最终的正则表达式

"([\w]{40}|[\d\.]+ [\d-]+ [\d:]+)$"

在以下网站进行测试:https://regex101.com/

对你有用吗?

================================================================
编辑:与键字段的上下文匹配

另一种选择是检查需要匹配的行是否以EntryIDVal开头:

(?:Entry |IDVal )(.*)

因此,你的代码应该如下所示:

pattern := '(?:Entry |IDVal )(.*)'
regex   := regexp.MustCompile(pattern)
matches := regex.FindAllStringSubmatch(str1, -1)

变量matches将包含长度为2的数组[[m1,g1], [m2,g2], ... ],其中每个列表将包含匹配m<i>和组g<i>。你的信息将包含在相应的组g<i>中。

英文:

You can start building your query from the back.

Case 1: 4273E6D162ED2717A1CF4207A254004CD3F5307B:

  • multiple alphanumerical characters of length 40, as hinted by your regex attempt: [\w]{40}

Case 2: 113.132.26.203 2022-12-28 12:56:30:

  • a set of numbers and ":": [\d:]+,
  • a space,
  • a set of numbers and "-": [\d-]+
  • a space,
  • a set of numbers and ".": [\d\.]+

Final regex:

&quot;([\w]{40}|[\d\.]+ [\d-]+ [\d:]+)$&quot;

Tested on: https://regex101.com/

Does it work for you?

================================================================
EDIT: matching with context of the key field

Another option is to check whether Entry or IDVal is at the beginning of the line you need to match:

(?:Entry |IDVal )(.*)

Hence, your code should look like the following:

pattern := &#39;(?:Entry |IDVal )(.*)&#39;
regex   := regexp.MustCompile(pattern)
matches := regex.FindAllStringSubmatch(str1, -1)

The variable matches will contain arrays of length two [[m1,g1], [m2,g2], ... ] where each list will contain the match m&lt;i&gt; and the group g&lt;i&gt;. Your information will be contained inside the corresponding group g&lt;i&gt;.

huangapple
  • 本文由 发表于 2022年4月14日 05:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/71863811.html
匿名

发表评论

匿名网友

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

确定