Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regular expression for exact string length

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

like image 941
user7399815 Avatar asked Mar 10 '23 16:03

user7399815


2 Answers

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
like image 163
RomanPerekhrest Avatar answered Mar 19 '23 16:03

RomanPerekhrest


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)

like image 25
Sebastian Proske Avatar answered Mar 19 '23 14:03

Sebastian Proske