python - Django正则表达式匹配长url

标签 python regex django

我有这么长的网址,我想匹配 ? 之后的字符串并在基于类的 View 的 self.kwargs 中提供它。

new_timer/?UID=046F1572564080&runtime=1102&seconds=30stillrunning=1&numstoredtimes=3&storedtimes=13:2-23:32-48:43&checksum=71

我尝试了以下方法,但不起作用。

Urlpatterns = [
    # bunch of awesome urls
    url(r'^new_timer/(?P<params>[^/]+)/$',NewTimerView.as_view(),
        name='new_timer'),
]

我做错了什么?

最佳答案

What am I doing wrong?

正则表达式中有两个错误:^new_timer/(?P<params>[^/]+)/$

  1. 您不匹配?根本不。此外,您还必须escape它。

  2. 您有/最后。而没有/在末尾的 URL 中。

正确的正则表达式应该是:^new_timer/\?(?P<params>[^/]+)$

<强> Regex101 Demo

关于python - Django正则表达式匹配长url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44120813/

相关文章:

python - SQLAlchemy+ Tornado : can't reconnect until invalid transaction is rolled back

Python-将边列表转换为邻接矩阵

python - 在 OpenFST (PyFST) 的弧中显示字符而不是整数

python - 正则表达式捕捉网址

regex - Perl6 : Capturing Windows newline in a string with regex

python - 如何查找某个特定字符串后的字符? [化学代码]

jquery - django 如何处理基于模式的登录/注册?

python - django/python - 正则表达式

python - django-rest-framework-bulk 批量更新查看输出定制

python - celery .delay 挂起(最近,不是授权问题)