Articles → Python → Starts With And Ends With Metacharacters In Regular Expression In Python

Starts With And Ends With Metacharacters In Regular Expression In Python






Starts With And Ends With Metacharacter


MetacharacterMeaning
^Starts with
$Ends with



Example


import re


start_with_pattern = "^\d"
end_with_pattern = "\d$"

text1 = r"my phone number is 12345"
text2 = r"12345 is my phone number"

print("Compare starts with pattern")
print("text1 compared with starts with:", re.search(start_with_pattern, text1))
print("text2 compared with starts with:", re.search(start_with_pattern, text2))

print("************************************")
print("Compare ends with pattern")
print("text1 compared with ends with:", re.search(end_with_pattern, text1))
print("text2 compared with ends with:", re.search(end_with_pattern, text2))



Output


Picture showing the output of starts with and ends with metacharacters in regular expression in python



Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, July 8, 2020

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250