My file contains either 45 hex numbers, separated by whitespaces or 48 hex numbers, separated by whitespaces. I need ALL of those numbers individually and not as a whole. I am currently using a brute force method to get 45 numbers.
pattern = re.compile("([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s([0-9a-f]{2})\s")
However, even with this, I still cant figure out how to extract the remaining three numbers in a 48 hex number instance. Could you please help me out with simplifying this problem?
I would avoid solutions like the ones below (haven’t tried if it works) as I will have to later split the string for each instance i.e. considering it gives proper output!
(((?:[0-9a-f]{2})\s){48})|(((?:[0-9a-f]{2})\s){45})
Thank you!
When writing long REs, consider using
re.VERBOSEto make them more readable.Read as: two hex digits, followed by 44 times (space followed by two hex digits), optionally followed by 3 times (space followed by two hex digits).
Test:
Then finally, to retrieve the individual digits, do
.group(0).split()on the match result. That’s much easier than writing an RE that puts all the digits into separate groups.EDIT: alright, here’s how to solve the original problem. Just construct the RE dynamically.