[Python] python re - match against first part of expression [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    For this expression:

    ^1[tpndovg][0-9]+


    And this string:

    1p


    I'd like to be able to say 'the string matches the first part of the expression, but does not match the entire expression'.

    Specifically, this deals with validation during typing into a Qt field, using a slot / callback connected to the QLineEdit.textChanged signal. Basically, this callback is run once per keystroke in the field.

    Example 1:

    First keystroke: 1 Is it a partial match? yes

    Next keystroke: p Is it a partial match? yes

    Example 2:

    First keystroke: 1 Is it a partial match? yes

    Next keystroke: 2 Is it a partial match? NO

    What are the options for accomplishing this? Is there a function in the re module or in the regex module that will do it? Or, would it require parsing the expression to make separate versions of the expression for one-character-typed, for two-characters-typed, for three-characters-typed, and so on?

    Continue reading...

Compartilhe esta Página