1

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...

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • \n is the new line character. There may not be a \ in that string. I'm guessing that it is just printing an empty string. – Robert Prévost Sep 10 '16 at 01:42
  • @RobertPrévost correct, but I don't get what you say. `subdir` is what I showed above, and `10973689-Parthenon` is what I want to extract. – gsamaras Sep 10 '16 at 01:53
  • Try executing print(subdir) or subdir.index('\n'). I'm getting at find_between_r(subdir, '/', '\n'). – Robert Prévost Sep 10 '16 at 01:58
  • The first gives me exactly the string I have in the question as input and the second 87. – gsamaras Sep 10 '16 at 02:00
  • are you sure your text actually contains `\ ` ? not just `\n` ? – njzk2 Sep 10 '16 at 02:11
  • njzk2, I think I know where the confusion comes from, let me update. @RobertPrévost check the update, now I understood what you were asking! ;) – gsamaras Sep 10 '16 at 02:17

1 Answers1

1

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
Mikhail M.
  • 5,588
  • 3
  • 23
  • 31