gsamaras gsamaras - 2 months ago 6
Linux Question

Find substring between / and \

After Get a list of subdirectories, I got strings of that form:

In [30]: print(subdir)
foousa 0 2016-09-07 19:58 /projects/foousa/obama/trump/10973689-Parthenon
drwx------ -


and when I try this answer:

In [23]: print find_between( subdir, "/", "\\" )


In [24]: print find_between( subdir, "\/", "\\" )


I don't get anything, maybe a newline only... What I was aiming for is
10973689-Parthenon
.

What am I missing?

I am using , but I couldn't see how this would matter...

DJV DJV
Answer

Using re:

import re

subdir = ' foousa          0 2016-09-07 19:58 /projects/foousa/obama/trump/10973689-Parthenon\ndrwx------   - '
match = re.search(r'/([^/\n]+)\n', subdir)
print(match.group(1))

Using indexes:

subdir = ' foousa          0 2016-09-07 19:58 /projects/foousa/obama/trump/10973689-Parthenon\ndrwx------   - '
begin = subdir.rindex('/') + 1
end = subdir.rindex('\n')
result = subdir[begin:end]
print(result)

output:

10973689-Parthenon
Comments