import re


def find_links(text, image_only=False):
    """
    Find Markdown links in text and return a match object.

    Markdown links are expected to have the form [some txt](A-url.ext)
    or ![Alt text](cool-image.png).

    Parameters
    ----------

    text : str
        Text in which to search for links.

    image_only : bool
        If ``True``, find only markdown image links, i.e. those that
        begin with an exclamation mark.

    Returns
    -------

    list
        List of ``re.Match`` objects, one for each link found. Each object
        has two named groups, 'link_text', which contains the the part between
        the square brackets, and 'link',which is the URL (or file name for an
        image).
    """
    if image_only:
        markdown_link = \
            re.compile(r"!\[(?P<link_text>.+?\n*?.*?)\]\((?P<link_url>.+?)\)",
                       flags=re.MULTILINE)
    else:
        markdown_link = \
            re.compile(r"!?\[(?P<link_text>.+?\n*?.*?)\]\((?P<link_url>.+?)\)",
                       flags=re.MULTILINE)
    groups = [m for m in markdown_link.finditer(text)]
    return groups