Python regular expression for exact string length

11,846

Solution 1

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

Solution 2

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)

Solution 3

Can you paste some example strings (each of length 32 and 40)?

Without the example I can think of making use of ^ and $ to match the beginning of the string and end of the string as explained in the Python re tutorial.

Example:

^[0-9a-fA-F]{32}$
Share:
11,846
user7399815
Author by

user7399815

Updated on June 13, 2022

Comments

  • user7399815
    user7399815 almost 2 years

    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