2

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.

4
  • "that one" is not deleted, you deleted a third one. (I commented on the third one, after deletion I was shown that you have asked "that one" as well.
    – cyberbrain
    Apr 13, 2022 at 21:23
  • Does this answer your question? Regex to process hexadecimal, ip and timestamp values in a flat file
    – cyberbrain
    Apr 13, 2022 at 21:23
  • 1
    @cyberbrain got it , yes kind of similar to the link to the question I had. But I want all Entries for an IDVal this time, instead of just the first occurence . Please refer the qn description for input string and expected output. Thanks for your time.
    – yin yang
    Apr 13, 2022 at 22:02
  • Can you handle groups and backreferences? @yinyang
    – lemon
    Apr 13, 2022 at 22:20

1 Answer 1

2

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>.

11
  • 1
    Thanks lemon, it worked :) . why do we need $ ( end of line or string in the regex ) ? . Also please tell me some good online tutorials apart from regexr/regex101 . I have done some online courses still struggling with regexes .
    – yin yang
    Apr 13, 2022 at 22:45
  • The end of line is needed otherwise [\d\w]{3}[\d\w]+ will match words like IDVal, Posted, Status, Entry. Though you can modify the regex by increasing the cardinality of [\d\w]{3} to say [\d\w]{6} or maybe match the exact number of letters/numbers if that is constant (and remove the $). Loved a lot the tutorial at regexone.com, check it out when you have time. Good to hear my help was useful!
    – lemon
    Apr 13, 2022 at 22:55
  • Not sure why it gives me false for : re.MatchString(data.String()) ( my data is byte buffer) . I do see that the above regex is for go so not sure why it returns false.
    – yin yang
    Apr 14, 2022 at 15:46
  • Can you share a snippet with the code, your input, your output and the expected output? I've never practised go language but maybe I can help you more on the topic with some extra info @yinyang
    – lemon
    Apr 14, 2022 at 15:51
  • Sure : go.dev/play/p/OHf08TB4pZa . I want to be able to get/print IP and timestamp for each Hexadecimal signature. Like mentioned in the "Output should match/capture :" section in description above. Thanks
    – yin yang
    Apr 14, 2022 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.