标签:pos block span possible imp desire return text add
https://www.cnblogs.com/xudong-bupt/p/3586889.html
*?, +?, ??
- The
‘*‘
,‘+‘
, and‘?‘
qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE<.*>
is matched against‘<a> b <c>‘
, it will match the entire string, and not just‘<a>‘
. Adding?
after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE<.*?>
will match only‘<a>‘
.
是否需要贪婪匹配在写正则表达式时是很重要的一个概念。‘?‘只有在上面三种表达时才表示非贪婪匹配,其他时候都是表示0个或1个字符。贪婪匹配简单来说就是匹配尽可能多符合的结果,非贪婪匹配表示只要匹配到一个符合的结果就行。下面这个程序是用来匹配前几个字符串是包含数字的的字符串,用来提取数字,数字可以包含正负号,如“+2356 wqrvg”:
1 import re 2 class Solution: 3 def myAtoi(self, str: str) -> int: 4 str = str.strip() 5 group = re.match(r‘(([-\+]\d+)|\d+).?‘, str) 6 if group is None: 7 return 0 8 temp = group.groups() 9 temp1 = int(temp[0]) 10 temp2 = pow(2, 31) 11 if temp1 > (temp2 - 1): 12 return temp2-1 13 elif(temp1 < (-1*temp2)): 14 return -1*temp2 15 return temp1
标签:pos block span possible imp desire return text add
原文地址:https://www.cnblogs.com/nanjingli/p/10597287.html