Python Regex Match

Aug 9, 2017
Use re.search

Assuming you want determine if a string match a certain pattern, re.search 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 reurl = "https://www.luasoftware.com/tutorials/python/python-regex-match.html"# regex for begins withm = re.search(r"^https://", url)if m:  print("is https")# regex for middle matchm = re.search(r"://([\w\d-]+\.)?luasoftware.com/", url)if m:  print("domain is luasoftware.com")# regex for ends withm = re.search(r"\.html$", url)if m:  print("file extension is html")

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.