[Python] Matching a pattern and a contiguous character but not the character by itself

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I want to use regex in python to match a pattern and optionally a character that could be present within or outside the pattern, but never solely the character.

    Eg. matching a word with ' potentially present before, in the middle, or at the end.

    The ideal pattern would match 'word, wo'rd, word', word, but should not match ' by itself.

    Is there a way to do it WITHOUT using something super chunky pattern like r"[a-z']*[a-z]+[a-z']*" or manipulating the data to get rid of matches with re.sub(r" '+ ", " ")

    I'm looking to craft a simple pattern similar to [\w']+ that would match a particular pattern and potentially a specified character(s) contiguous to it but not match the specified characters when not contiguous to the pattern.

    Continue reading...

Compartilhe esta Página