python-regexHow to match a URL path using Python regex?
Python regex can be used to match a URL path. The re
module provides a re.match()
function which takes a regular expression pattern and a string and attempts to match the pattern to the string.
import re
url = 'https://www.example.com/path/to/file'
match = re.match('^https?://www\.example\.com/path/to/file$', url)
if match:
print('Match found!')
else:
print('No match found!')
Output example
Match found!
Code explanation
import re
: imports there
module which provides functions for working with regular expressionsre.match()
: takes a regular expression pattern and a string and attempts to match the pattern to the string^
: matches the beginning of the string$
: matches the end of the string
Helpful links
More of Python Regex
- How to replace all using Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match a year with Python Regex?
- How to use word boundaries in Python Regex?
- How to match a UUID using Python regex?
- How to replace a certain group using Python regex?
- How to match a question mark in Python regex?
- How to match one or more occurence in Python regex?
- How to make a case insensitive match with Python regex?
- How to match zero or one occurence in Python regex?
See more codes...