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 replace all using Python regex?
- How to match zero or one occurence in Python regex?
- How to get a group from a regex in Python?
- How to match HTML tags with regex in Python?
- How to get all matches from a regex in Python?
- How to validate an IP using Python regex?
- How to perform a zero length match with Python Regex?
- How to use word boundaries in Python Regex?
- How to match a hex number with regex in Python?
- How to match a float with regex in Python?
See more codes...