Skip to content

Instantly share code, notes, and snippets.

@tianchu
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tianchu/d05b390e023ed6007e89 to your computer and use it in GitHub Desktop.
Save tianchu/d05b390e023ed6007e89 to your computer and use it in GitHub Desktop.
Useful Python regular expressions
URL_REGEX = re.compile(r"^(http|https)://.+$")
IP_ADDRESS_V4_REGEX = re.compile(r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}"
r"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
US_ZIP_REGEX = re.compile(r"\d{5}(\-\d{4})?$")
UK_ZIP_REGEX = re.compile(r"^((GIR ?0AA)|((([A-PR-UWYZ][A-HK-Y]?[0-9][0-9]?)"
r"|(([A-PR-UWYZ][0-9][A-HJKSTUW])"
r"|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRV-Y]))) ?"
r"[0-9][ABD-HJLNP-UW-Z]{2}))$")
CANADA_ZIP_REGEX = re.compile(r"^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$")
PHONE_NUM_REGEX = re.compile(r"^\(?\+?[\d\(\-\s\)]+$")
EMAIL_REGEX = re.compile(r"[^@]+@[^@]+\.[^@]+")
LANG_CODE_REGEX = re.compile(r"^[a-z]{2}(_[A-Z]{2})?$") # E.g., en_US or cn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment