开发者

How to trim the longest match from beginning of a string (using python)

In recent bash versions, I can do this:

$ string="Universe.World.Country.State.City.Street"
$ echo $string
Universe.World.Country.State.City.Street
$ newStri开发者_如何学运维ng="${string##*.}"
$ echo $newString
Street

Using Python, what is a succinct way to doing the same? I am interested in the last substring after the last period.

Thank you!


How about

x[x.rfind('.') + 1 : ]

To me, that expresses what you're interested in (find the last dot, then take everything after it) more simply than a pattern or the concept of a "longest match".


>>> 'Universe.World.Country.State.City.Street'.rpartition('.')[2]
'Street'


>>> "Universe.World.Country.State.City.Street".rsplit('.',1)[1]
'Street'

Edit: rpartition as suggested by SilentGhost seems to be the most efficient

# rpartition
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x.rpartition(".")[-1]'
100 loops, best of 100: 0.749 usec per loop

# rfind
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x[x.rfind(".")+1:]'
100 loops, best of 100: 0.808 usec per loop

# rsplit
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x.rsplit(".",1)[1]'
100 loops, best of 100: 0.858 usec per loop

# split
$ python -m timeit -r100 -n100 -s 'x="Universe.World.Country.State.City.Street"' 'x.split(".")[-1]'
100 loops, best of 100: 1.26 usec per loop

# regex
$ python -m timeit -r100 -n100 -s 'import re;rex=re.compile(r"\.([^.]*)$");x="Universe.World.Country.State.City.Street"' 'rex.search(x).groups()[0]'
100 loops, best of 100: 3.16 usec per loop


Maybe:

re.search(r"\.([^.]*)$", s).groups()[0]

EDIT: First version was bad :)


If you know its always going to be the last element and a full stop you can't beat

"Universe.World.Country.State.City.Street".split(".")[-1]


string.rsplit('.', 1)[-1] with maxsplit=1 returns only the rightmost '.', so it also only matches once.

Looks like a tossup with string.rpartition('.')[-1]

PS: gnibbler actually timed rsplit and it was slightly slower than rpartition, rfind.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜