import re


def strip_comments(text):
    """
    Remove comment lines (those starting with #) and leading/trailing
    whitespace from ``text``.
    """
    
    # (m?) enables multiline mode
    return re.sub(r'(?m)^ *#.*\n?', '', text).strip()