An interesting question, I would suggest doing something like this:
>>> 'Docs/src/Scripts/temp'.replace('/', '/\x00/').split('\x00') ['Docs/', '/src/', '/Scripts/', '/temp']
The idea here is to first replace all characters /
with two characters /
, separated by a special character that would not be part of the original string. I used a null byte ( '\x00'
), but you could change it to something else, and then split it into this special character.
Regex is actually not very good because you cannot split into zero-length matches, and re.findall()
does not find matching matches, so you may need to do a few passes over the line.
In addition, re.split('/', s)
will do the same as s.split('/')
, but the second is more efficient.
source share