import re


_VID_PID_PATTERN = re.compile(r"vid_([0-9a-f]{4})&pid_([0-9a-f]{4})", re.IGNORECASE)


def normalize_device_name(value):
    text = str(value or "").strip()
    return text


def extract_device_match_token(device_name):
    text = normalize_device_name(device_name).lower()
    if not text:
        return ""
    match = _VID_PID_PATTERN.search(text)
    if match:
        vid = match.group(1).lower()
        pid = match.group(2).lower()
        return f"hid#vid_{vid}&pid_{pid}"
    return text
