Search code examples
pythonregexpython-2.7directory

Python regex matching all but last occurrence


So I have expression such as "./folder/thisisa.test/file.cxx.h" How do I substitute/remove all the "." but the last dot?


Solution

  • To match all but the last dot with a regex:

    '\.(?=[^.]*\.)'
    

    Using a lookahead to check that's there another dot after the one we found (the lookahead's not part of the match).