<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _match_elements(dom, matches):
 """ Find location of elements matching patterns specified in `matches`. Args: dom (obj):
 HTMLElement DOM tree. matches (dict):
 Structure: ``{"var": {"data": "match", ..}, ..}``. Returns: dict: Structure: ``{"var": {"data": HTMLElement_obj, ..}, ..}`` """

    out = {}
    for key, content in matches.items():
        pattern = content["data"].strip()
        if "\n" in pattern:
            pattern = pattern.split()
            transformer = lambda x: x.strip().split()
        else:
            transformer = lambda x: x.strip()

        matching_elements = _locate_element(
            dom,
            pattern,
            transformer=transformer
        )

        not_found_msg = content.get("notfoundmsg", "").replace("$name", key)
        if not not_found_msg.strip():
            not_found_msg = "Can't locate variable '%s' with content '%s'!" % (
                key,
                pattern,
            )
        content["notfoundmsg"] = not_found_msg

        # in case of multiple elements, find only elements with propert tagname
        tagname = content.get("tagname", "").strip().lower()
        if tagname:
            matching_elements = filter(
                lambda x: x.getTagName().strip().lower() == tagname,
                matching_elements
            )

        if not matching_elements:
            raise UserWarning(not_found_msg)

        if len(matching_elements) > 1:
            raise UserWarning(
                "Ambigious content '%s'!" % content
                + "Content was found in multiple elements!"
            )

        out[key] = matching_elements[0]

    return out