如何获得此字符串的正确正则表达式?

时间:2012-10-11 14:22:09

标签: python regex url

当我将其作为描述参数传递时(包括其后面的返回字符和URL),这里有一些文本无法正常工作。我在django做这个。

  

搭档/ BuzzFeed / fXkqhhllOtA / NY洋基队:季后赛纪念品的6件必备品/国家棒球名人堂展示了6件洋基队的季后赛纪念品:1923年世界大赛的一枚表; Babe Ruth的蝙蝠来自1926年的世界大赛; Yogi Berra的手套来自Don Larsen在1956年的完美比赛; 1962年世界大赛的最后一球; 1996年世界大赛的Derek Jeter球衣;来自2000年地铁系列的Mariano Rivera的帽子。 http://www.buzzfeed.com/sports/

urlpatterns = patterns('reserve.views',
    url(r'^partner/(?P<partner_name>[-\w]+)/$', 'partner_channel'),
    url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/$', 'video_player'),
    url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<title>.+)/(?P<desc>.+)/$', 'video_player'),
    url(r'^category/(?P<category>[-\w]+)/$', 'all_partners'),
    url(r'^admin/', include(admin.site.urls)),
)

如何更改desc参数的正则表达式以允许此操作?

编辑:

未找到请求网页(404):

http:/localhost:8000/partner/BuzzFeed/fXkqhhIlOtA/NY%20Yankees:%206%20Essential%20Pieces%20of%20Postseason%20Memorabilia/The%20National%20Baseball%20Hall%20of%20Fame%20shows%20off%206%20pieces%20of%20Yankees%20postseason%20memorabilia:%20a%20watch%20from%20the%201923%20World%20Series;%20Babe%20Ruth's%20bat%20from%20the%201926%20World%20Series;%20Yogi%20Berra's%20glove%20from%20Don%20Larsen's%20perfect%20game%20in%201956;%20the%20last%20out%20ball%20in%20the%201962%20World%20Series;%20Derek%20Jeter's%20jersey%20from%20the%201996%20World%20Series;%20Mariano%20Rivera's%20hat%20from%20the%202000%20Subway%20Series.%0A%0Ahttp://www.buzzfeed.com/sports/

3 个答案:

答案 0 :(得分:0)

问题是你的标题匹配是贪婪的,匹配的比你想要的多,并且/或是分隔你的部分的/被包含在标题中,而desc是最后一个/之后的所有内容(在url中)< / p>

将其更改为非贪婪(?P<title>.+?)

url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<title>.+?)/(?P<desc>.+)/$', 'video_player'),

答案 1 :(得分:0)

给它s选项。默认情况下,.匹配换行符之外的任何字符。此外,您需要使您的标题部分不贪婪,否则它将与您的整个描述相匹配。使用.+?代替.+来执行此操作。

答案 2 :(得分:0)

您的urlpatterns被覆盖(可能不正确)。您有两种模式匹配'video_player'但没有匹配'desc':

url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/$', 'video_player'),
url(r'^partner/(?P<author>[-\w]+)/(?P<video>[-\w]+)/(?P<title>.+)/(?P<desc>.+)/$', 'video_player'),

将上一个网址标识符从上方更改为“video_player”以外的其他内容。