Skip to main content

Regular Expression

Character types
  • \w matches with any alphanumeric character
  • . matches to all characters, including symbols

  • \d matches to all single digits [0-9]

  • \s matches to all single spaces 

  • \. matches to the period character

import re
re.findall("\w", "h32rb17")

import re
re.findall("\d", "h32rb17")
Quantify occurrences
  • + symbol represents one or more occurrences of a specific character.
  • * symbol represents zero, one, or more occurrences of a specific character.
  • \d{2} instructs Python to return all matches of exactly two single digits
  • \d{1,3} 數字 1 - 3 位數
import re
re.findall("\d+", "h32rb17")

import re
re.findall("\d*", "h32rb17")

import re
re.findall("\d{2}", "h32rb17 k825t0m c2994eh")

import re
re.findall("\d{1,3}", "h32rb17 k825t0m c2994eh")
import re
pattern = "\w+:\s\d+"
employee_logins_string = "1001 bmoreno: 12 Marketing 1002 tshah: 7 Human Resources 1003 sgilmore: 5 Finance"
print(re.findall(pattern, employee_logins_string))
['bmoreno: 12', 'tshah: 7', 'sgilmore: 5']