[Python] python regex: match the dot only, not the letter before it

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a regex pattern as follows:

    r'(?:(?<!\.|\s)[a-z]\.|(?<!\.|\s)[A-Z]\.)+'


    and I am trying to modify that so it only matches the dot at the end of the sentences and not the letter before them. here is my string:

    sent = 'This is the U.A. we have r.a.d. golden 13.56 date. a better date 34. was there.'


    and here is what i have done:

    import re
    re.split(r'(?:(?<!\.|\s)[a-z]\.|(?<!\.|\s)[A-Z]\.)+', sent)


    however what happens is that it removes the last letter of the words:

    current output:
    ['This is the U.A. we have r.a.d. golden 13.56 dat',' a better date 34. was ther',
    '']


    my desired output is:

    ['This is the U.A. we have r.a.d. golden 13.56 date',' a better date 34. was there',
    '']


    i do not know how I can modify the pattern to keep the last letter of the words 'date', and 'there'

    Continue reading...

Compartilhe esta Página