Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 传递4个数字以查看_Python_Regex_Django - Fatal编程技术网

Python 传递4个数字以查看

Python 传递4个数字以查看,python,regex,django,Python,Regex,Django,这不起作用的原因: (r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)(?:(?P<id4>\d+)/)?$', 'something'), 我当前的代码在传递3或4个参数时有效,而不是2 怎么了?在正则表达式中,第三个数字组是必需的 ^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)?(?

这不起作用的原因:

(r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)(?:(?P<id4>\d+)/)?$', 'something'),
我当前的代码在传递3或4个参数时有效,而不是2


怎么了?

在正则表达式中,第三个数字组是必需的

^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)?(?:(?P<id4>\d+)/)?$
^something/(?P\d+)/(?P\d+)/(?:(?P\d+)/)(?:(?P\d+)/)$
此外,如前所述,第三组和第四组后面的斜杠是强制性的:

In [4]: p = re.compile(r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)?(?:(?P<id4>\d+)/)?$')

In [5]: p.match('something/1/2/3/4/').groups()
Out[5]: ('1', '2', '3', '4')

In [6]: p.match('something/1/2/3/4').groups()
Out[6]: ('1', '2', '3', None)

In [7]: p.match('something/1/2/3/').groups()
Out[7]: ('1', '2', '3', None)

In [8]: p.match('something/1/2/3').groups()
Out[8]: ('1', '2', None, None)

In [9]: p.match('something/1/2/').groups()
Out[9]: ('1', '2', None, None)
[4]中的
:p=re.compile(r'^something/(?p\d+)/(?p\d+)/(?:(?p\d+)/)?(?:(?p\d+)/)?$)
在[5]中:p.match('something/1/2/3/4/')。groups()
Out[5]:('1','2','3','4')
在[6]中:p.match('something/1/2/3/4')。groups()
Out[6]:('1','2','3',无)
[7]:p.match('something/1/2/3/')。groups()
Out[7]:('1','2','3',无)
在[8]中:p.match('something/1/2/3')。groups()
Out[8]:('1','2',无,无)
[9]:p.match('something/1/2/').groups()
Out[9]:('1','2',无,无)
您可能希望将第三个组移动到第四个组,并使第四个组成为可选组:

In [12]: p = re.compile(r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+))?(?:/(?P<id4>\d+)/?)?$')

In [13]: import re
KeyboardInterrupt

In [13]: p.match('something/1/2/3/4').groups()
Out[13]: ('1', '2', '3', '4')

In [14]: p.match('something/1/2/3/').groups()
Out[14]: ('1', '2', '3', None)

In [15]: p.match('something/1/2/3').groups()
Out[15]: ('1', '2', '3', None)

In [16]: p.match('something/1/2/').groups()
Out[16]: ('1', '2', None, None)
[12]中的
:p=re.compile(r'^something/(?p\d+)/(?p\d+)/(?:(?p\d+))?(?:/(?p\d+)/)?$)
在[13]中:输入re
键盘中断
[13]:p.match('something/1/2/3/4')。groups()
Out[13]:('1','2','3','4')
在[14]中:p.match('something/1/2/3/')。groups()
Out[14]:('1','2','3',无)
在[15]中:p.match('something/1/2/3')。groups()
Out[15]:('1','2','3',无)
在[16]中:p.match('something/1/2/').groups()
Out[16]:('1','2',无,无)

第三个数字组在正则表达式中是必需的

^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)?(?:(?P<id4>\d+)/)?$
^something/(?P\d+)/(?P\d+)/(?:(?P\d+)/)(?:(?P\d+)/)$
此外,如前所述,第三组和第四组后面的斜杠是强制性的:

In [4]: p = re.compile(r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+)/)?(?:(?P<id4>\d+)/)?$')

In [5]: p.match('something/1/2/3/4/').groups()
Out[5]: ('1', '2', '3', '4')

In [6]: p.match('something/1/2/3/4').groups()
Out[6]: ('1', '2', '3', None)

In [7]: p.match('something/1/2/3/').groups()
Out[7]: ('1', '2', '3', None)

In [8]: p.match('something/1/2/3').groups()
Out[8]: ('1', '2', None, None)

In [9]: p.match('something/1/2/').groups()
Out[9]: ('1', '2', None, None)
[4]中的
:p=re.compile(r'^something/(?p\d+)/(?p\d+)/(?:(?p\d+)/)?(?:(?p\d+)/)?$)
在[5]中:p.match('something/1/2/3/4/')。groups()
Out[5]:('1','2','3','4')
在[6]中:p.match('something/1/2/3/4')。groups()
Out[6]:('1','2','3',无)
[7]:p.match('something/1/2/3/')。groups()
Out[7]:('1','2','3',无)
在[8]中:p.match('something/1/2/3')。groups()
Out[8]:('1','2',无,无)
[9]:p.match('something/1/2/').groups()
Out[9]:('1','2',无,无)
您可能希望将第三个组移动到第四个组,并使第四个组成为可选组:

In [12]: p = re.compile(r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+))?(?:/(?P<id4>\d+)/?)?$')

In [13]: import re
KeyboardInterrupt

In [13]: p.match('something/1/2/3/4').groups()
Out[13]: ('1', '2', '3', '4')

In [14]: p.match('something/1/2/3/').groups()
Out[14]: ('1', '2', '3', None)

In [15]: p.match('something/1/2/3').groups()
Out[15]: ('1', '2', '3', None)

In [16]: p.match('something/1/2/').groups()
Out[16]: ('1', '2', None, None)
[12]中的
:p=re.compile(r'^something/(?p\d+)/(?p\d+)/(?:(?p\d+))?(?:/(?p\d+)/)?$)
在[13]中:输入re
键盘中断
[13]:p.match('something/1/2/3/4')。groups()
Out[13]:('1','2','3','4')
在[14]中:p.match('something/1/2/3/')。groups()
Out[14]:('1','2','3',无)
在[15]中:p.match('something/1/2/3')。groups()
Out[15]:('1','2','3',无)
在[16]中:p.match('something/1/2/').groups()
Out[16]:('1','2',无,无)

您在那里忘记了一个问号…您在那里忘记了一个问号…您的代码可以工作,但允许4个以上的参数。我需要限制为最多4个。对不起,我只是错拍了
$
。修正。你的代码可以工作,但允许超过4个参数。我需要限制为最多4个。对不起,我只是错拍了
$
。固定的。