Python - The most elegant way to extract a substring by giving left and right borders

I have a line - Python:

string = "/foo13546897/bar/Atlantis-GPS-coordinates/bar457822368/foo/" 

Expected Result:

 "Atlantis-GPS-coordinates" 

I know that the expected result is ALWAYS surrounded by "/ bar /" on the left and "/" on the right:

 "/bar/Atlantis-GPS-coordinates/" 

The proposed solution will look like this:

 a = string.find("/bar/") b = string.find("/",a+5) output=string[a+5,b] 

It works, but I don't like it. Does anyone know a great feature or tooltip?

+6
source share
4 answers

You can use split :

 >>> string.split("/bar/")[1].split("/")[0] 'Atlantis-GPS-coordinates' 

Some efficiency from adding max split 1 I suppose:

 >>> string.split("/bar/", 1)[1].split("/", 1)[0] 'Atlantis-GPS-coordinates' 

Or use partition :

 >>> string.partition("/bar/")[2].partition("/")[0] 'Atlantis-GPS-coordinates' 

Or regex:

 >>> re.search(r'/bar/([^/]+)', string).group(1) 'Atlantis-GPS-coordinates' 

Depends on what tells you and your data.

+7
source

What you have is not so bad. I would write this as:

 start = string.find('/bar/') + 5 end = string.find('/', start) output = string[start:end] 

as long as you know that /bar/WHAT-YOU-WANT/ will always be present. Otherwise, I could apply for a regexp knife :

 >>> import re >>> PATTERN = re.compile('^.*/bar/([^/]*)/.*$') >>> s = '/foo13546897/bar/Atlantis-GPS-coordinates/bar457822368/foo/' >>> match = PATTERN.match(s) >>> match.group(1) 'Atlantis-GPS-coordinates' 
+3
source
 import re pattern = '(?<=/bar/).+?/' string = "/foo13546897/bar/Atlantis-GPS-coordinates/bar457822368/foo/" result = re.search(pattern, string) print string[result.start():result.end() - 1] # "Atlantis-GPS-coordinates" 

This is an example of Python 2.x. The first thing he does: 1. (? <= / Bar /) means only process the next regular expression if it precedes it (so / bar / should be before it) 2. '. +? / 'Means any number of characters until the next' / 'char

Hope this helps.

If you need to perform such a search, you should better โ€œcompileโ€ this performance search, but if you only need to do this, do not worry.

+1
source

Using re (slower than other solutions):

 >>> import re >>> string = "/foo13546897/bar/Atlantis-GPS-coordinates/bar457822368/foo/" >>> re.search(r'(?<=/bar/)[^/]+(?=/)', string).group() 'Atlantis-GPS-coordinates' 
0
source

All Articles