I'm using Django's URLconf, the URL I will receive is /?code=authenticationcode
I want to match the URL using r'^\?code=(?P<code>.*)$'
, but it doesn't work.
Then I found out it is the problem of '?'.
Becuase I tried to match /aaa?aaa
using r'aaa\?aaa'
r'aaa\\?aaa'
even r'aaa.*aaa'
, all failed, but it works when it's "+" or any other character.
How to match the '?', is it special?
But if you want to search a question mark, you need to “escape” the regex interpretation of the question mark. You accomplish this by putting a backslash just before the quesetion mark, like this: \? If you want to match the period character, escape it by adding a backslash before it.
The question mark quantifier indicates that you want to match either one or zero occurrences of this pattern. The second part of the regex [cde]? defines a character class [cde] which reads as “match either c , d , or e “. Again, the question mark indicates the zero-or-one matching requirement.
The question mark symbol has a special meaning in Python regular expressions: its the zero-or-one quantifier of the preceding regex. You can get rid of the special meaning of the question mark symbol by using the backslash prefix: \? . This way, you can match the question mark symbol characters in a given string.
A regular expression followed by a question mark (?) matches zero or one occurrences of the regular expression. Two regular expressions concatenated match an occurrence of the first followed by an occurrence of the second.
>>> s="aaa?aaa"
>>> import re
>>> re.findall(r'aaa\?aaa', s)
['aaa?aaa']
The reason /aaa?aaa
won't match inside your URL is because a ?
begins a new GET query.
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.
What you can do here is encode the variable before it makes its way into the URL. The encoded form of ?
is %3F
.
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')
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With