-2

I'm using Python to do a very simple regex on a domain. I can't figure out why line 5 below doesn't return a match - what am I doing wrong?

In [1]: import re

In [2]: a = re.compile("example\.com", re.IGNORECASE)

In [3]: a.match("example.com")
Out[3]: <_sre.SRE_Match at 0x10b3a3b90>

In [4]: a.match("example.com/dlkfdsf")
Out[4]: <_sre.SRE_Match at 0x10b3a36b0>

In [5]: a.match("http://www.example.com/sdklfjsdf")
# No match
imichaeldotorg
  • 459
  • 1
  • 4
  • 20
  • 1
    because match stries to match from the begining of a string. change your regex to `r'.*example\.com'` or use `re.search` – Avinash Raj Jun 29 '15 at 13:31

2 Answers2

0

As Avinash Raj already suggested in comments, match matches the full text.

Try this:

import re

a = re.compile("(example\.com)", re.IGNORECASE)

a.search("http://www.example.com/sdklfjsdf").groups(1)
('example.com',)
firelynx
  • 30,616
  • 9
  • 91
  • 101
-1

I should have been using re.search().

imichaeldotorg
  • 459
  • 1
  • 4
  • 20