[关闭]
@zhangyu756897669 2017-09-11T15:13:55.000000Z 字数 2059 阅读 866

zxcvbnm,./管理复杂的正则表达式

python官方文档


  1. phoneRegex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}
  2. (\s*(ext|x|ext.)\s*\d{2,5})?)')
  1. phoneRegex = re.compile(r'''(
  2. (\d{3}|\(\d{3}\))? # area code
  3. (\s|-|\.)? # separator
  4. \d{3} # first 3 digits
  5. (\s|-|\.) # separator
  6. \d{4} # last 4 digits
  7. (\s*(ext|x|ext.)\s*\d{2,5})? # extension
  8. )''', re.VERBOSE)

组合reignignecase,re.DOTALL和re.VERBOSE

  1. someRegexValue = re.compile('foo', re.IGNORECASE | re.DOTALL)
  1. someRegexValue = re.compile('foo', re.IGNORECASE | re.DOTALL | re.VERBOSE)

项目:电话号码和电子邮件地址提取器

步骤1:创建电话号码的正则表达式

  1. #! python3
  2. # phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.
  3. import pyperclip, re
  4. phoneRegex = re.compile(r'''(
  5. (\d{3}|\(\d{3}\))? # area code
  6. (\s|-|\.)? # separator
  7. (\d{3}) # first 3 digits
  8. (\s|-|\.) # separator
  9. (\d{4}) # last 4 digits
  10. (\s*(ext|x|ext.)\s*(\d{2,5}))? # extension
  11. )''', re.VERBOSE)
  12. # 创建电子邮件正则表达式。
  13. # 在剪贴板文本中查找匹配。
  14. # 将结果复制到剪贴板
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注