Advertisement
here2share

Some common regular expressions (regex) examples in Python

Dec 23rd, 2022
1,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.57 KB | None | 0 0
  1. Some common regular expressions (regex) examples in Python
  2.  
  3. Matching a single character:
  4.  
  5. import re
  6.  
  7. Match any digit (0-9)
  8. re.findall(r"\d", "abc123")
  9.  
  10. Match any lowercase letter (a-z)
  11. re.findall(r"[a-z]", "abc123")
  12.  
  13. Match any uppercase letter (A-Z)
  14. re.findall(r"[A-Z]", "ABC123")
  15.  
  16. Match any letter (a-z, A-Z)
  17. re.findall(r"[a-zA-Z]", "ABC123")
  18. Matching a specific string:
  19.  
  20. import re
  21.  
  22. Match the string "cat"
  23. re.findall(r"cat", "The cat in the hat")
  24.  
  25. Match the string "dog"
  26. re.findall(r"dog", "The cat in the hat")
  27.  
  28. Matching a pattern of characters:
  29.  
  30. import re
  31.  
  32. Match any string that starts with "cat"
  33. re.findall(r"^cat", "cat in the hat")
  34.  
  35. Match any string that ends with "dog"
  36. re.findall(r"dog$", "The cat and the dog")
  37.  
  38. Match any string that contains "hat"
  39. re.findall(r"hat", "The cat in the hat")
  40. Matching repeated patterns:
  41.  
  42. import re
  43.  
  44. Match any string that has two consecutive digits
  45. re.findall(r"\d\d", "abc123")
  46.  
  47. Match any string that has three consecutive lowercase letters
  48. re.findall(r"[a-z]{3}", "abc123")
  49.  
  50. Match any string that has three or more consecutive digits
  51. re.findall(r"\d{3,}", "abc123")
  52.  
  53. Match any string that has two or more consecutive lowercase letters
  54. re.findall(r"[a-z]{2,}", "abc123")
  55. Matching optional patterns:
  56.  
  57. import re
  58.  
  59. Match any string that has an optional digit at the end
  60. re.findall(r"\d?", "abc123")
  61.  
  62. Match any string that has an optional lowercase letter at the end
  63. re.findall(r"[a-z]?", "abc123")
  64.  
  65. Match any string that has an optional digit at the start
  66. re.findall(r"^\d?", "abc123")
  67.  
  68. Match any string that has an optional lowercase letter at the start
  69. re.findall(r"^[a-z]?", "abc123")
  70. Matching groups of patterns:
  71.  
  72. import re
  73.  
  74. Match any string that has a word (consisting of letters) followed by a digit
  75. re.findall(r"\b\w+\b\d", "abc123")
  76.  
  77. Match any string that has a digit followed by a word (consisting of letters)
  78. re.findall(r"\d\b\w+\b", "abc123")
  79. Here are a few more examples:
  80.  
  81. Matching multiple patterns:
  82.  
  83. import re
  84.  
  85. Match any string that has a word (consisting of letters) followed by a digit, and then another word
  86. re.findall(r"\b\w+\b\d\b\w+\b", "abc123def")
  87.  
  88. Matching multiple patterns:
  89.  
  90. import re
  91.  
  92. Match any string that has a digit, followed by a word (consisting of letters), followed by another digit
  93. re.findall(r"\d\b\w+\b\d", "abc123def")
  94.  
  95. Match any string that has a word (consisting of letters) followed by a digit, or a digit followed by a word
  96. re.findall(r"\b\w+\b\d|\d\b\w+\b", "abc123def")
  97.  
  98. Match any string that has a word (consisting of letters) followed by a digit, or a digit followed by a word, or both
  99. re.findall(r"\b\w+\b\d|\d\b\w+\b|\b\w+\b\d\b\w+\b", "abc123def")
  100.  
  101. Matching multiple occurrences:
  102.  
  103. import re
  104.  
  105. Match any string that has three or more consecutive occurrences of the same letter
  106. re.findall(r"(\w)\1{2,}", "abc123def")
  107.  
  108. Match any string that has three or more consecutive occurrences of the same digit
  109. re.findall(r"(\d)\1{2,}", "abc123def")
  110.  
  111. Match any string that has two or more consecutive occurrences of the same letter, separated by a digit
  112. re.findall(r"(\w)\d(\1)", "abc123def")
  113.  
  114. Match any string that has two or more consecutive occurrences of the same digit, separated by a letter
  115. re.findall(r"(\d)\w(\1)", "abc123def")
  116.  
  117. Matching any character:
  118.  
  119. import re
  120.  
  121. Match any string that has a single character
  122. re.findall(r".", "abc123def")
  123.  
  124. Match any string that has two consecutive characters
  125. re.findall(r"..", "abc123def")
  126.  
  127. Match any string that has three consecutive characters
  128. re.findall(r"...", "abc123def")
  129.  
  130. Match any string that has four consecutive characters
  131. re.findall(r"....", "abc123def")
  132.  
  133. Matching any digit:
  134.  
  135. import re
  136.  
  137. Match any string that has a single digit
  138. re.findall(r"\d", "abc123def")
  139.  
  140. Match any string that has two consecutive digits
  141. re.findall(r"\d\d", "abc123def")
  142.  
  143. Match any string that has three consecutive digits
  144. re.findall(r"\d\d\d", "abc123def")
  145.  
  146. Match any string that has four consecutive digits
  147. re.findall(r"\d\d\d\d", "abc123def")
  148.  
  149. Matching any letter:
  150.  
  151. import re
  152.  
  153. Match any string that has a single letter
  154. re.findall(r"[a-zA-Z]", "abc123def")
  155.  
  156. Match any string that has two consecutive letters
  157. re.findall(r"[a-zA-Z][a-zA-Z]", "abc123def")
  158.  
  159. Match any string that has three consecutive letters
  160. re.findall(r"[a-zA-Z][a-zA-Z][a-zA-Z]", "abc123def")
  161.  
  162. Matching any letter:
  163.  
  164. import re
  165.  
  166. Match any string that has four consecutive letters
  167. re.findall(r"[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]", "abc123def")
  168.  
  169. Match any string that has two or more consecutive letters
  170. re.findall(r"[a-zA-Z]{2,}", "abc123def")
  171.  
  172. Match any string that has three or more consecutive letters
  173. re.findall(r"[a-zA-Z]{3,}", "abc123def")
  174.  
  175. Match any string that has four or more consecutive letters
  176. re.findall(r"[a-zA-Z]{4,}", "abc123def")
  177.  
  178. Matching any word:
  179.  
  180. import re
  181.  
  182. Match any string that has a single word
  183. re.findall(r"\b\w+\b", "abc 123 def")
  184.  
  185. Match any string that has two consecutive words
  186. re.findall(r"\b\w+\b\s\b\w+\b", "abc 123 def")
  187.  
  188. Match any string that has three consecutive words
  189. re.findall(r"\b\w+\b\s\b\w+\b\s\b\w+\b", "abc 123 def")
  190.  
  191. Match any string that has four consecutive words
  192. re.findall(r"\b\w+\b\s\b\w+\b\s\b\w+\b\s\b\w+\b", "abc 123 def")
  193.  
  194. Match any string that has two or more consecutive words
  195. re.findall(r"\b\w+\b\s{1,}\b\w+\b", "abc 123 def")
  196.  
  197. Match any string that has three or more consecutive words
  198. re.findall(r"\b\w+\b\s{1,}\b\w+\b\s{1,}\b\w+\b", "abc 123 def")
  199.  
  200. Match any string that has four or more consecutive words
  201. re.findall(r"\b\w+\b\s{1,}\b\w+\b\s{1,}\b\w+\b\s{1,}\b\w+\b", "abc 123 def")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement