import re


def get_sde_id(pattern, string, n=1):
    """Extract an id from an sde string. Return the first part of string that matches pattern, or the whole string."""
    m = re.search(pattern, string)
    if m is not None:
        return m.group(n)
    return string