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()
...
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)
('',)