// plain
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.
import re
line = "This is the end of the line"
if re.search("end$", line):
print("Match found")
Match found
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.
onelinerhub: How to match the end of a line with regex in Python?