You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
2.6KB

  1. import re
  2. '''
  3. 1.匹配某个字符串
  4. match():只能匹配某个
  5. '''
  6. # text = 'python python'
  7. # result = re.match('py', text)
  8. # print(result)
  9. # print(result.group())
  10. '''
  11. 2.点
  12. 匹配任意的某个字符【无法匹配换行符】【必须从开头开始匹配】
  13. '''
  14. # text = 'python'
  15. # result = re.match('.', text)
  16. # print(result)
  17. # print(result.group())
  18. '''
  19. 3.\d
  20. 匹配任意的数字【除了数字外均无法匹配】
  21. '''
  22. # text = '1python'
  23. # result = re.match('\d', text)
  24. # print(result)
  25. # print(result.group())
  26. '''
  27. 4.\D
  28. 除了数字外均可匹配【数字均无法匹配】
  29. '''
  30. # text = 'python'
  31. # result = re.match('\D', text)
  32. # print(result)
  33. # print(result.group())
  34. '''
  35. 5.\s
  36. 匹配空白字符【\n、 \t、 \r 、空格】
  37. '''
  38. # text = '\npython'
  39. # result = re.match('\s', text)
  40. # print(result)
  41. # print(result.group())
  42. '''
  43. 6.\w
  44. 匹配小写的a-z,大写的A-Z,数字和下划线
  45. '''
  46. # text = 'python'
  47. # result = re.match('\w', text)
  48. # print(result)
  49. # print(result.group())
  50. '''
  51. 7.\W
  52. 匹配除小写\w之外的所有字符
  53. '''
  54. # text = '\npython'
  55. # result = re.match('\W', text)
  56. # print(result)
  57. # print(result.group())
  58. '''
  59. 8.[] ->> 组合的方式
  60. 只要在中括号内的内容均可匹配
  61. '''
  62. # text = '\npython'
  63. # result = re.match('[\n_]', text)
  64. # print(result)
  65. # print(result.group())
  66. '''
  67. 9.星号 *
  68. 匹配零个或者多个字符
  69. '''
  70. # text = '139-1234-5678'
  71. # result = re.match('[-\d]*', text)
  72. # print(result)
  73. # print(result.group())
  74. '''
  75. 10.加号 +
  76. 匹配1个或者多个字符
  77. '''
  78. # text = '139-1234-5678'
  79. # result = re.match('[-\d]+', text)
  80. # print(result)
  81. # print(result.group())
  82. '''
  83. 11.问号 ?
  84. 匹配0个或者匹配1个
  85. '''
  86. # text = '139-1234-5678'
  87. # result = re.match('[-\d]?', text)
  88. # print(result)
  89. # print(result.group())
  90. '''
  91. 12. {m,n} 匹配m到n个
  92. 匹配0个或者匹配1个
  93. '''
  94. # text = '139-1234-5678'
  95. # result = re.match('[-\d]{1,5}', text)
  96. # print(result)
  97. # print(result.group())
  98. '''
  99. 13. 匹配所有的数字
  100. '''
  101. # text = '139-1234-5678'
  102. # result = re.match('[-0-9]*', text)
  103. # print(result)
  104. # print(result.group())
  105. '''
  106. 14. 匹配所有的非数字
  107. '''
  108. # text = '139-1234-5678'
  109. # result = re.match('[^0-9]*', text)
  110. # print(result)
  111. # print(result.group())
  112. # re.match() 必须从字符串开头进行匹配
  113. # re.search() 从左到右进行字符串的遍历,找到就返回
  114. # text = 'aapython'
  115. # result = re.match('py', text)
  116. # print(result)
  117. # print(result.group())
  118. # text = 'aapython'
  119. # result = re.search('py', text)
  120. # print(result)
  121. # print(result.group())
  122. """
  123. 1. 贪婪模式: 正则表达式会尽可能多地匹配字符【默认就是贪婪模式】
  124. 2. 非贪婪模式: 正则表达式会尽可能少地匹配字符【?】
  125. 转义字符 \
  126. """