2
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??

Albin Antony
  • 775
  • 3
  • 8
  • 21

2 Answers2

7

Obligatory "what if you didn't use regex?" answer:

>>> url="www.example.com/thedubaimall"
>>> url.partition(".com/")[2]
'thedubaimall'
Kevin
  • 74,910
  • 12
  • 133
  • 166
5

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.

Psidom
  • 209,562
  • 33
  • 339
  • 356
  • 2
    I believe that the regex can be greatly simplified to `'com/(.+)'` (or `'com/(.*)'`, based on whether OP wants an empty list or an empty string for `'www.example.com/'`) – DeepSpace Jul 01 '16 at 13:36