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
Metacharacter | Meaning |
---|
^ | 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