On Fri, Jul 25, 2008 at 8:23 AM, timc3 <[EMAIL PROTECTED]> wrote:
> Just isn't working, and it looks like a RegEx problem to me, but I am
> not sure why..

It's usually best to explain what isn't working, exactly. Is it just
not being matched, and you're getting a 404? Is your urls.py giving
you errors? Is it matching but not filling the variables with the
right values? There's not a lot to go on here.

> I am trying to match the following URL:
>
> http://127.0.0.1:8000/services/oembed/?ref=http://127.0.0.1:8000/media/slug/guid/

First, you have a problem. URL patterns don't match query strings, so
Django will only pass "services/oembed/" into the pattern matching
logic. If you want the whole thing to be matched, so you can filter
out the specific pieces (which, below, it looks like you do), you'll
need to drop the "?ref=" and try to match something like this instead:

http://127.0.0.1:8000/services/oembed/http://127.0.0.1:8000/media/slug/guid/

> And the regex that I am using:
>
> urlpatterns = patterns('',
>    url(r'^oembed/(?P<http>[-\w]+)//(?<site>[-\w]+)/media/(?P<slug>[-
> \w]+)/(?P<guid>[-\w]+)/$', 'brokersite.services.views.mediaoembed'),
> )

Wow. Okay, a few things:

* First, I'm getting a syntax error when I try to compile this regex
directly. You're missing a "P" after the question mark in your <site>
group.

* You're also missing a colon after the <http> group, so it's
expecting to match "http//" instead of "http://";.

* Your <http> group also doesn't need to be a full [-\w+]. If you're
truly only asking for HTTP addresses, it can be much more specific,
which will serve you better in the long run. Consider:
(?P<http>https?). That'll match both "http" and "https" but not things
like "ftp", "aim", "gopher", etc. Also, dashes aren't allowed in the
protocol anyway, so that part can go away easily. Perhaps better yet,
you could go with "http(?P<secure>s?)", so you'd get a "secure"
argument that you can easily use in things like "if secure:" later on.

* Your <site> group, on the other hand, is matching far too little
what you're after. For starters, \w doesn't match periods, so you'll
never match a valid domain or IP address that way. Also, since your
example includes a port number to match, you also need to check for
colons. Try something like this, (?P<site>[-:\.\w]+), though I still
can't guarantee that'll match everything you might come up against.

Basically, it looks like you saw an example somewhere of using [-\w]+
to match slugs, and thought it was a useful general-purpose pattern to
use, which isn't the case. Regular expressions can be complicated
things if you want them to be, and you're certainly asking for a
complicated case. Each group needs to be written for the content it'll
actually match against, so you need to be prepared for it. Try doing
some reading on regular expressions elsewhere to learn some of these
finer details.

Also, though, I have to ask why you're using such a complicated URL
pattern in the first place? For some applications, I can understand
accepting a full URL, but your pattern is expecting something very
specific, which means you're not looking to accept just any arbitrary
URL. Why bother using the full URL at all then? Couldn't you just
append a portion of it instead, and infer the rest in your code? If
you're after a full, arbitrary URL, your regex is far too specific at
this point.

Anyway, I hope I helped at least a little bit.

-Gul

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to