如果要使用正则表达式,请使用re.findall:re.findall('(?<=com/).*$', "www.example.com/thedubaimall")
# ['thedubaimall']
一些速度测试有@DeepSpace的建议:%timeit re.findall('(?<=com/).*$', "www.example.com/thedubaimall")
# The slowest run took 7.57 times longer than the fastest. This could mean that an intermediate result is being cached.
# 1000000 loops, best of 3: 1.29 µs per loop
%timeit re.findall('com/(.*)', "www.example.com/thedubaimall")
# The slowest run took 6.48 times longer than the fastest. This could mean that an intermediate result is being cached.
# 1000000 loops, best of 3: 992 ns per loop
%timeit "www.example.com/thedubaimall".partition(".com/")[2]
# The slowest run took 7.87 times longer than the fastest. This could mean that an intermediate result is being cached.
# 1000000 loops, best of 3: 204 ns per loop
看起来,@DeepSpace的建议快了一点,@Kevin的回答快多了。