且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带问号文字的Python正则表达式

更新时间:2023-02-22 12:56:37

>>> s="aaa?aaa"
>>> import re
>>> re.findall(r'aaa\?aaa', s)
['aaa?aaa']

/ aaa?aaa 在您的URL中不匹配的原因是因为以新的GET查询。

The reason /aaa?aaa won't match inside your URL is because a ? begins a new GET query.

因此,URL的可匹配部分仅取决于第一个 aaa。其余的?aaa是一个新的查询字符串,以?号分隔,其中包含一个变量 aaa作为GET参数传递。

So, the matchable part of the URL is only up to the first 'aaa'. The remaining '?aaa' is a new query string separated by the '?' mark, containing a variable "aaa" being passed as a GET parameter.

您在这里 所能做的就是在变量进入URL之前对其进行编码。 的编码形式为%3F

What you can do here is encode the variable before it makes its way into the URL. The encoded form of ? is %3F.

您也不应该完全使用正则表达式来匹配GET查询,例如 /?code = authenticationcode 。而是使用 r’^ $’将您的URL匹配到 / 。 Django会将变量 code 作为GET参数传递给 request 对象,您可以在视图中使用 request.GET.get('code')

You should also not match a GET query such as /?code=authenticationcode using regex at all. Instead, match your URL up to / using r'^$'. Django will pass the variable code as a GET parameter to the request object, which you can obtain in your view using request.GET.get('code').