'Regex to process multiple entries for a digital signature
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.
Solution 1:[1]
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:
"([\w]{40}|[\d\.]+ [\d-]+ [\d:]+)$"
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 := '(?:Entry |IDVal )(.*)'
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<i> and the group g<i>. Your information will be contained inside the corresponding group g<i>.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
