[Python] Split and keep deliminator, preferably with regex

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

  1. Stack

    Stack Membro Participativo

    Let's say I have this text:

    1.1 This is the 2,1 first 1.2 This is the 2,2 second 1.3 This is the 2,3 third


    and I want:

    ["1.1 This is the 2,1 first","1.2 This is the 2,2 second","1.3 This is the 2,3 third"]


    Note that:


    • I can't use re.findall, since I can't think of a way to properly terminate the match. The best I could think of was '[0-9]+\.[0-9]+^([0-9]+\.[0-9]+)*', which didn't work.


    • I can't just store the delimiter as a global variable, since it changes with each match.


    • I could not use a regular re.split because I want to keep the delimiter. I can't use a lookbehind because it has to be fixed width, and this isn't.

    I have read regexp split and keep the seperator, Python split() without removing the delimiter, and In Python, how do I split a string and keep the separators?, and still don't have an answer.

    Continue reading...

Compartilhe esta Página