赞
踩
以下三种可以匹配包括换行符在内的任意字符:
[\s\S]* 、 [\d\D]*、 [\w\W]*
示例代码如下:
- import re
-
- line = 'ver_loss:1.386276092529297;ver_p_ana:0.500774740.860.50096285nan;ver_n_ana:0.50076320.15875nan0.5009661;pp_loss:6.360285034179688;pn_loss:6.539781494140625;'
- pattern1 = re.compile(r'ver_loss:(\w*.\w+);[\s\S]*pp_loss:(\w*.\w+);pn_loss:(\w*.\w+);')
- pattern2 = re.compile(r'ver_loss:(\w*.\w+);[\d\D]*pp_loss:(\w*.\w+);pn_loss:(\w*.\w+);')
- pattern3 = re.compile(r'ver_loss:(\w*.\w+);[\w\W]*pp_loss:(\w*.\w+);pn_loss:(\w*.\w+);')
- print(pattern1.findall(line))
- print(pattern2.findall(line))
- print(pattern3.findall(line))
-
- # output:
- # [('1.386276092529297', '6.360285034179688', '6.539781494140625')]
- # [('1.386276092529297', '6.360285034179688', '6.539781494140625')]
- # [('1.386276092529297', '6.360285034179688', '6.539781494140625')]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。