当前位置:   article > 正文

python regexp_python regexp {0}

python regexp {0}

Simplest case -


  1. import re
  2. regexp = re.compile('hello')
  3. count = 0
  4. file = open('j.txt','r')
  5. for line in file.readlines() :
  6. if regexp.search(line) :
  7. count = count + 1
  8. file.close()
  9. print(count)


Now, if you want to use wildcard pattern.


  1. import re
  2. regexp = re.compile('(h|H)ello')
  3. count = 0
  4. file = open('j.txt','r')
  5. for line in file.readlines() :
  6. if regexp.search(line) :
  7. count = count + 1
  8. file.close()
  9. print(count)

If you want to search '\ten', you need use raw strings in pattern.  

regexp = re.complie('\\ten')

In this way, you have escape '\' . Other char - \t (tab) \n (a new line) ...

>>> r"Hello" == "Hello" True

>>> r"\the"  == "\\the" True

>>> r"\the" == "\the" False



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

闽ICP备14008679号