I am trying to read the contents of a file and check if matches list of patterns using regular expression.
File content:
google.com
https://google.com
yahoo.com
www.yahoo.com
yahoo
My code:
import re
file = 'data_files/test_files/content.txt'
regex_1 = re.compile("google")
regex_2 = re.compile("yahoo")
data = open(file, 'r')
print ("Checking Regex 1")
if regex_1.match(data.read()):
count_c = len(regex_1.findall(data.read()))
print ("Matched Regex 1 - " + str(count_c))
print("Checking Regex 2")
if regex_2.match(data.read()):
count_d = len(regex_2.findall(data.read()))
print("Matched Regex 2 - " + str(count_d))
else:
print ("No match found")
Output:
Checking Regex 1
Checking Regex 2
No match found
Couldn't figure out what is wrong here.