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 theremodule which contains there.search()function.line = "This is the end of the line": assigns the string to thelinevariable.if re.search("end$", line):: checks if thelinevariable contains theendstring at the end of the line using there.search()function.print("Match found"): prints theMatch foundstring if theendstring is found at the end of the line.
Helpful links
More of Python Regex
- How to match a question mark in Python regex?
- How to match whitespace in Python regex?
- How to match a plus sign in Python regex?
- How to match a YYYY-MM-DD date with Python Regex?
- How to match one or more occurence in Python regex?
- How to perform a zero length match with Python Regex?
- How to match a URL using Python regex?
- How to match a hex color with regex in Python?
- How to match a year with Python Regex?
- How to regex match excluding a word in Python?
See more codes...