import re
import yaml


def get_yaml(note, key):
    """
    Find a YAML property in a note.
    (Gets the first occurrence of a property.)
    You can also pass a regex pattern matching a key.
    """
    body = note.body.split('\n')
    i = 0
    while i<len(body):
        if (type(key)==str and body[i].startswith(key)) or (type(key)==re.Pattern and key.search(body[i])):
            #print(i)
            yaml_content = body[i]
            while i+1<len(body) and (body[i+1].startswith(' ') or body[i+1].startswith('\t')):
                    yaml_content += '\n'+body[i+1]
                    i += 1
            #print(i)
            return(yaml.safe_load(yaml_content)[key])
        i += 1
    return(None)