import re
from pathlib import Path


def get_bibtex(filename):
    """ Tries to extract a bibtex key from a filename and returns it.
        If no bibtex key is found the function returns the filename (without suffix). """

    bibtex = re.match(r'^[a-zA-Z]*\d{4}[a-z]?', Path(filename).stem)
    if bibtex:
        bibtex = bibtex.group()
    else:
        bibtex = Path(filename).stem
    return bibtex