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 perform a zero length match with Python Regex?
- How to match a URL using Python regex?
- How to match HTML tags with regex in Python?
- How to replace in a file using Python regex?
- How to use word boundaries in Python Regex?
- How to match whitespace in Python regex?
- How to remove numbers from a string using Python regex?
- How to match zero or one occurence in Python regex?
- How to match a UUID using Python regex?
- How to split using Python regex?
See more codes...