xxxxxxxxxx
import re
# Target String
target_string = "Emma is a baseball player who was born on June 17, 1993."
# find substring 'ball'
result = re.search(r"ball", target_string)
# Print matching substring
print(result.group())
# output 'ball'
# find exact word/substring surrounded by word boundary
result = re.search(r"\bball\b", target_string)
if result:
print(result)
# output None
# find word 'player'
result = re.search(r"\bplayer\b", target_string)
print(result.group())
# output 'player'
xxxxxxxxxx
def search_keyword(filename, keyword):
with open(filename, "r") as file:
lines = file.readlines()
for i, line in enumerate(lines):
if keyword in line:
print("Line number:", i + 1)
print("Line:", line)
filename = input("Enter the file name: ")
keyword = input("Enter the keyword to search: ")
search_keyword(filename, keyword)