import re


def scan(file, blocksize=10000):
    """Get the number of molecules in an sd file"""
    pat = re.compile("^[$][$][$][$]", re.MULTILINE)

    text = file.read(blocksize)
    count = 0
    while text:
        g = pat.findall(text)
        count += len(g)
        if text[-1] == "$" and text[-4]!='$':
            next = text[-6:]
            text = "".join([next, file.read(blocksize)])
        else:
            text = text = file.read(blocksize)

    return count