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

Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250