Python Regex Match

August 9, 2017

Assuming you want determine if a string match a certain pattern, is your best bet. You might think of using re.match, but the use case is more specific as it match the beginning of the string (or the whole string).

import re

url = ""

# regex for begins with
m ="^https://", url)
if m:
  print("is https")

# regex for middle match
m ="://([\w\d-]+\.)?", url)
if m:
  print("domain is")

# regex for ends with
m ="\.html$", url)
if m:
  print("file extension is html")
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.