Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 正则表达式:(.*)、(.*)和*_Python_Regex - Fatal编程技术网

Python 正则表达式:(.*)、(.*)和*

Python 正则表达式:(.*)、(.*)和*,python,regex,Python,Regex,有人能给我解释一下这三个街区之间的区别吗: 1 -> (.*) 2 -> (.*?) 3 -> .* 据我所知,?使得最后一个字符是可选的,那么为什么不把它放在末尾呢 这来自这里: *将匹配任何字符(如果使用了dotall,则包括换行符)。这是贪婪的:它尽可能匹配 (.*)将其添加到捕获组中 (.*?这个?使得*不贪婪,尽可能少地匹配以进行匹配,而括号也使它成为一个捕获组 例如: >>> import re >>> txt = ''' fo

有人能给我解释一下这三个街区之间的区别吗:

1 -> (.*)
2 -> (.*?)
3 -> .*
据我所知,
使得最后一个字符是可选的,那么为什么不把它放在末尾呢

这来自这里:


*
将匹配任何字符(如果使用了dotall,则包括换行符)。这是贪婪的:它尽可能匹配

(.*)
将其添加到捕获组中

(.*?
这个
使得
*
不贪婪,尽可能少地匹配以进行匹配,而括号也使它成为一个捕获组

例如:

>>> import re
>>> txt = ''' foo
... bar
... baz '''
>>> for found in re.finditer('(.*)', txt):
...     print found.groups()
... 
(' foo',)
('',)
('bar',)
('',)
('baz ',)
('',)
>>> for found in re.finditer('.*', txt):
...     print found.groups()
... 
()
()
()
()
()
()
>>> for found in re.finditer('.*', txt, re.DOTALL):
...     print found.groups()
... 
()
()
>>> for found in re.finditer('(.*)', txt, re.DOTALL):
...     print found.groups()
... 
(' foo\nbar\nbaz ',)
('',)
由于
尽可能少地匹配,因此我们匹配空字符串:

>>> for found in re.finditer('(.*?)', txt, re.DOTALL):
...     print found.groups()
... 
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)

例如:将
(.*)h
“this thing”
匹配将返回
“this t”
(可能以h结尾的最长字符串);匹配
(.*)h
将返回
“t”
(尽可能短的以h结尾的字符串)。谢谢大家,还有非常有趣的链接。我现在明白了,英国人把“贪婪”定义为一个付出很多的人,而法国人把“贪婪”定义为一个付出/回报最少的人
>>> for found in re.finditer('(.*?)', txt, re.DOTALL):
...     print found.groups()
... 
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)