defmatch(pattern, string, flags=0): """Try to apply the pattern at the start of the string, returning a match object, or None if no match was found.""" return _compile(pattern, flags).match(string)
defsearch(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.""" return _compile(pattern, flags).search(string)
参数
pattern 正则表达式
string 字符串
flags 编译标志位,用于修改正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。
匹配对象方法
group() 获取匹配到的所有结果
groups() 获取模型中匹配到的分组结果
groupdict() 获取模型中匹配到的分组结果
start() 返回匹配开始的位置
end() 返回匹配结束的位置
span() 返回一个元组包含匹配 (开始,结束) 的位置
1 2 3 4 5 6 7 8 9 10 11
import re
r = re.search('\dcom', 'www.4comrunoob.5com') print(r.group()) print(r.groups()) print(r.groupdict())
deffindall(pattern, string, flags=0): """Return a list of all non-overlapping matches in the string. If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.""" return _compile(pattern, flags).findall(string)
参数
pattern 正则表达式
string 字符串
flags 编译标志位,用于修改正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 无分组
import re
r = re.findall(r'\d+', '1one1two2three3four4') print(r)
# 有分组 r = re.findall(r'\d+(..)', '1one1two2three3four4') print(r)
r = re.finditer(r'\d+', '1one1two2three3four4') print(r) for i in r: print(i.span(), i.group(), i.groupdict()) ------------ <callable_iterator object at 0x101178748> (0, 1) 1 {} (4, 5) 1 {} (8, 9) 2 {} (14, 15) 3 {} (19, 20) 4 {}
sub
sub,替换匹配成功的指定位置字符串
1 2 3 4 5 6 7 8
defsub(pattern, repl, string, count=0, flags=0): """Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.""" return _compile(pattern, flags).sub(repl, string, count)
defsplit(pattern, string, maxsplit=0, flags=0): """Split the source string by the occurrences of the pattern, returning a list containing the resulting substrings. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list.""" return _compile(pattern, flags).split(string, maxsplit)
defchengshufajisuan(s): """ 专门用来计算不带括号的计算表达式中的乘除法 :param s: 不带括号的计算表达式 :return: 只有加减法的计算表达式(如果不再有加减法,直接返回计算结果) """ whileTrue: if'*'in s or'/'in s: r = re.split(r'(\d+\.?\d*[*|/]-?\d+\.?\d*)', s, 1) ss = eval(r[1]) s = r[0] + str(ss) + r[2] else: break return s
defjiajianfajisuan(s): """ 专门用来计算不带括号的计算表示式中的加减法 :param s: 不带括号且只有加减法的表达式 :return: 最终的计算结果 """ s = fuhao(s) whileTrue: # 5-9的情况;-5-9的情况都需要额外考虑 if'+'in s or s.count('-') > 1or (s.count('-') == 1andnot s.startswith('-')): r = re.split(r'(-?\d+\.?\d*[+|-]\d+\.?\d*)', s, 1) ss = eval(r[1]) s = r[0]+str(ss)+r[2] else: break return s
defcal(s): """ + - * / 混合运算 :param s: :return: """ if'*'in s or'/'in s: s = chengshufajisuan(s) if'+'in s or s.count('-') > 1: s = jiajianfajisuan(s) return s else: return s elif'+'in s or s.count('-') > 1or (s.count('-') == 1andnot s.startswith('-')): s = jiajianfajisuan(s) return s else: returnNone
# print(cal(qukuohao(test_expression)))
if __name__ == '__main__': try: whileTrue: e = input('输入计算表达式> ') if'('in e: print(cal(qukuohao(e))) else: print(cal(e)) except KeyboardInterrupt: print('Bye')