I’m trying to write a code using regex and my text file. My file contains these words line by line:
nana
abab
nanac
eded
My purpose is; displaying the words which are not contain the letters which are given a substring’s letters.
For example, if my substring is “bn”, my output should be only eded. Because nana and nanac contains “n” and abab contains “b”.
I have written a code but it only checks first letter of my substring.
import re
substring = "bn"
def xstring():
with open("deneme.txt") as f:
for line in f:
for word in re.findall(r'\w+', line):
for letter in substring:
if len(re.findall(letter, word)) == 0:
print(word)
#yield word
xstring()
How do I solve this problem?
Solution:
If you want to check if a string has a set of letters, use brackets.
For example using [bn] will match words that contain one of those letters.
import re
substring = "bn"
regex = re.compile('[' + substring + ']')
def xstring():
with open("dename.txt") as f:
for line in f:
if(re.search(regex, line) is None):
print(line)
xstring()