当前位置:   article > 正文

正则表达式匹配任意字符(包括换行符)_正则表达式匹配包括换行符

正则表达式匹配包括换行符

以下三种可以匹配包括换行符在内的任意字符:

[\s\S]*   、 [\d\D]*、 [\w\W]*

示例代码如下:

  1. import re
  2. 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;'
  3. pattern1 = re.compile(r'ver_loss:(\w*.\w+);[\s\S]*pp_loss:(\w*.\w+);pn_loss:(\w*.\w+);')
  4. pattern2 = re.compile(r'ver_loss:(\w*.\w+);[\d\D]*pp_loss:(\w*.\w+);pn_loss:(\w*.\w+);')
  5. pattern3 = re.compile(r'ver_loss:(\w*.\w+);[\w\W]*pp_loss:(\w*.\w+);pn_loss:(\w*.\w+);')
  6. print(pattern1.findall(line))
  7. print(pattern2.findall(line))
  8. print(pattern3.findall(line))
  9. # output:
  10. # [('1.386276092529297', '6.360285034179688', '6.539781494140625')]
  11. # [('1.386276092529297', '6.360285034179688', '6.539781494140625')]
  12. # [('1.386276092529297', '6.360285034179688', '6.539781494140625')]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/635937
推荐阅读
相关标签
  

闽ICP备14008679号