url="www.example.com/thedubaimall"
I want to save everything that comes aftercom/
that is I need only thedubaimall
How can i do it with regular expression??
url="www.example.com/thedubaimall"
I want to save everything that comes aftercom/
that is I need only thedubaimall
How can i do it with regular expression??
Obligatory "what if you didn't use regex?" answer:
>>> url="www.example.com/thedubaimall"
>>> url.partition(".com/")[2]
'thedubaimall'
If you want to use regular expression, you use re.findall
:
re.findall('(?<=com/).*$', "www.example.com/thedubaimall")
# ['thedubaimall']
Some speed tests with @DeepSpace's suggestion:
%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
Looks like @DeepSpace's suggestion is a bit faster, and @Kevin's answer is much faster.