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 perform a zero length match with Python Regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match a UUID using Python regex?
- How to replace all using Python regex?
- How to use word boundaries in Python Regex?
- How to match a hex number with regex in Python?
- How to count matches with Python regex?
- How to match a year with Python Regex?
- How to split using Python regex?
See more codes...