from typing import Optional
import re


def _id_from_xpath(string: str) -> Optional[str]:
    """Return a service id from a given xpath string.

    Parameters
    ----------
    string: str
        String to check

    Returns
    -------
    Optional[str]
        Return env variable name if the pattern is detected

    Notes
    -----
    "/nano:nano/nano:nano[nano:id='123']"
    """
    pattern = re.compile(r"/nano:nano/nano:nano\[nano:id='(?P<id>[a-zA-Z0-9_-]+)'\]")
    match = pattern.fullmatch(string)
    return match.group("id") if match else None