python-regexHow to match the end of a line with regex in Python?
The $
character in a regular expression matches the end of a line. To match the end of a line with regex in Python, use the re.search()
function with the $
character in the regex pattern.
Example code
import re
line = "This is the end of the line"
if re.search("end$", line):
print("Match found")
Output example
Match found
Code explanation
import re
: imports there
module which contains there.search()
function.line = "This is the end of the line"
: assigns the string to theline
variable.if re.search("end$", line):
: checks if theline
variable contains theend
string at the end of the line using there.search()
function.print("Match found")
: prints theMatch found
string if theend
string is found at the end of the line.
Helpful links
More of Python Regex
- How to match a YYYY-MM-DD date with Python Regex?
- How to match a UUID using Python regex?
- How to match a year with Python Regex?
- How to replace all using Python regex?
- How to use word boundaries in Python Regex?
- How to remove numbers from a string using Python regex?
- How to get a group from a regex in Python?
- How to match a URL path using Python regex?
- How to use quantifiers in Python regex?
- How to quote in Python regex?
See more codes...