在/和\之间查找子字符串

时间:2016-09-10 01:35:49

标签: python linux string apache-spark substring

Get a list of subdirectories之后,我得到了那种形式的字符串:

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

当我尝试这个answer时:

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


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

我没有得到任何东西,也许只是换行...我的目标是10973689-Parthenon

我错过了什么?

我正在使用,但我看不出这有多重要......

1 个答案:

答案 0 :(得分:1)

使用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))

使用索引:

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)

输出:

10973689-Parthenon