I am trying to write a regex that will get a hash currently I have the following regex that catches MD5 hashes
[0-9a-fA-F].{32}
However, this will also get the first 32 characters of a longer string such as a SHA-1 hash. I am wondering how I can edit this to ensure that it only matches if the string is 32 characters long and not 40 characters?
EDIT: Sorry I should have said I am using Python 2.7
To match md5 hash as a whole string use start/end of the string anchors ^, $:
s = "3b4e1a15682994ef0bb2cbea8abfa105"
result = re.search(r'^[0-9a-fA-F]{32}$', s)
print result.group()   # 3b4e1a15682994ef0bb2cbea8abfa105
To match md5 hash as a substring(part of the text) use word boundaries \b:
s = "hash 3b4e1a15682994ef0bb2cbea8abfa105 some text"
result = re.search(r'\b[0-9a-fA-F]{32}\b', s)
print result.group()    # 3b4e1a15682994ef0bb2cbea8abfa105
                        There is a little (but all to important) mistake in your regex - [0-9a-fA-F].{32} matches one hex character and then 32 of any characters (except newline). Your pattern should thus be [0-9a-fA-F]{32}
To check that the whole string is matched you can either use re.fullmatch (added in Python 3.4) or use anchors ^ (start of the string) and $ (end of the string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With