[Python] Tell if a Python regex string has a lookaround

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I'd like to be able to reliably tell if a Python regex string contains a lookahead or lookbehind. It seems to me the code:

    bool(re.search(r'\(\?=[^)]*\)|\(\?!', regex_string))


    should match when a string has a lookahead, and similarly

    bool(re.search(r"\(\?<!|\(\?<=", regex_string))


    for lookbehind.

    However I am a causal user of regex, and wonder if there might be some other way to have a lookahead or lookbehind in a Python regex string that is not straightforward. Thus I'm hoping to get an expert eye on our current idea.

    Context: This is for the pandas library, where for PyArrow arrays the underlying compute engine does not implement lookaheads / lookbehinds. Thus we need to implement an alternate path, but for performance we'd like to only do this in these particular cases. See https://github.com/pandas-dev/pandas/issues/60833 for more details.

    Continue reading...

Compartilhe esta Página