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

2024-04-25 18:14:05 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一些文本,当我把它作为参数传递给description(包括后面的返回字符和url)时,它不起作用。我在django做这个。你知道吗

partner/BuzzFeed/fXkqhhIlOtA/NY Yankees: 6 Essential Pieces of Postseason Memorabilia/The National Baseball Hall of Fame shows off 6 pieces of Yankees postseason memorabilia: a watch from the 1923 World Series; Babe Ruth's bat from the 1926 World Series; Yogi Berra's glove from Don Larsen's perfect game in 1956; the last out ball in the 1962 World Series; Derek Jeter's jersey from the 1996 World Series; Mariano Rivera's hat from the 2000 Subway Series. 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参数的regex以允许此操作?你知道吗

编辑:

找不到请求URL页(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/

Tags: oftheinfromhttpurlworldpartner
3条回答

给它s选项。默认情况下,.匹配除新行以外的任何字符。另外,你需要使你的标题部分不贪婪,因为否则它将匹配你的整个描述。使用.+?而不是.+这样做。你知道吗

您的urlpatterns正在被覆盖(可能是不正确的)。有两个模式匹配“video\u 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'),

将上面的最后一个url标识符更改为“视频播放器”以外的内容。你知道吗

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

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

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

相关问题 更多 >