[Python] Regex match only if odd number of a character precede and follow

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I want to match the closing quote together with the opening quote of the following string if both are on the same line. Two strings may be separated either by a blank or a blank-plus-blank + .

    Regex engine: Python

    F.i. from

    this is "some string" "; which should match" 234
    "and this" + "should also match\"" "\"and this"
    but not this: " " a " + "


    I'd like to see matches for:

    • line 1: " " from between some string and ; which...
    • line 2:
      • " + " from between and this and should also match\"
      • " " from between should also match\" and \"and this
    • line 3: No matches

    So in fact, I think it might be best to only match the groups " " and " + " if there is an odd number of quotes before and after the group. Since lookbehing/ahead is fixed length only, I didn't find a good way to do it.

    I tried

    re.compile(r'(" \+ ")|(" ")(?!;|,)')


    but this assumes that there may be no semicolon within a string

    and also

    re.compile(r'"[^"]+")


    but this only finds the strings themselves, but not the "inter-string" quotes.

    Continue reading...

Compartilhe esta Página