简体   繁体   中英

How to extract a substring from inside a string?

Let's say I have a string /Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherthing I want to extract just the '0-1-2-3-4-5' part. I tried this:

str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'

print str[str.find("-")-1:str.find("-")]

But, the result is only 0. How to extract just the '0-1-2-3-4-5' part?

Use os.path.basename and rsplit:

>>> from os.path import basename
>>> name = '/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'
>>> number, tail = basename(name).rsplit('-', 1)
>>> number
'0-1-2-3-4-5'

You're almost there:

str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'
print str[str.find("-")-1:str.rfind("-")]

rfind will search from the end. This assumes that no dashes appear anywhere else in the path. If it can, do this instead:

str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'
str = os.path.basename(str)
print str[str.find("-")-1:str.rfind("-")]

basename will grab the filename, excluding the rest of the path. That's probably what you want.

Edit:

As pointed out by @bradley.ayers, this breaks down in the case where the filename isn't exactly described in the question. Since we're using basename , we can omit the beginning index:

print str[:str.rfind("-")]

This would parse '/Apath1/Bpath2/Cpath3/10-1-2-3-4-5-something.otherhing' as '10-1-2-3-4-5'.

This works:

>>> str='/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'
>>> str.split('/')[-1].rsplit('-', 1)[0]
'0-1-2-3-4-5'

Assuming that what you want is just what's between the last '/' and the last '-'. The other suggestions with os.path might make better sense (as long as there is no OS confusion over what aa proper path looks like)

you could use re :

>>> import re
>>> ss = '/Apath1/Bpath2/Cpath3/0-1-2-3-4-5-something.otherhing'
>>> re.search(r'(?:\d-)+\d',ss).group(0)
'0-1-2-3-4-5'

While slightly more complicated, it seems like a solution similar to this might be slightly more robust...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM