Check if line exists in text file

So, I have a:

def CheckUserExists(user): with open("C:/~/database.txt", 'r') as file: if re.search(user, file.read()): return True else: return False username = input("Please enter you Username: ") if CheckUserExists(username) == True: print("You exist!") else: print("This user does not exist...") 

However, if you enter, for example, the letter "a", and the user is called "brain"; the search raises a and returns True. How to search for whole words?

I looked here: How to check in Python if a line is in a text file and print the line? However, I do not understand the code snippet:

 re.search("\b{0}\b".format(w),line) 
+6
source share
4 answers

The regular expression \b refers to the empty line at the word boundary , where the word \w+ or [A-Za-z0-9_]+

If you have one name per line (without any other spaces around the names), you can search the line with ^{0}$ with the re.M or re.MULTILINE

It will look like this:

 def CheckUserExists(user): with open("C:/~/database.txt", 'r') as file: if re.search('^{0}$'.format(re.escape(user)), file.read(), flags=re.M): return True else: return False username = input("Please enter you Username: ") if CheckUserExists(username): # it redundant to check if == True here print("You exist!") else: print("This user does not exist...") 

Although the comment and answer suggest if you do

 if user in file.read() 

You may have false positives.

+3
source

To check if a word exists in the file, separated by a space:

 with open(filename) as file: found = (word in file.read().split()) 

Or the same thing, but reading line by line, rather than loading everyone in memory:

 with open(filename) as file: found = any(word in line.split() for line in file) 

If the file format is one word (/ user) per line:

 with open(filename) as file: found = any(word == line.strip() for line in file) 

You do not need regular expressions in simple cases. If there can be several words in a string, and in this case there can be arbitrary punctuation, you can use the regular expression associated with it:

 import re matched = re.compile(r"\b" + re.escape(word) + r"\b").search with open(filename) as file: found = any(matched(line) for line in file) 

\b regular expression matches the word boundary (start or end of a word). Symbols of a word are letters, numbers and the underscore. re.escape() used if the word contains regular expression metacharacters such as * .

+1
source

The line of code you are referencing is a regular expression . Basically, what he does in this case ensures that the word boundary (denoted by \b ) exists around the search string, which will prevent a substring from matching like the one you see.

0
source

Regex seems too complicated for the task ... I will use .split() to split each line in the file

 def CheckUserExists(user): with open("C:/~/database.txt", 'r') as file: for line in file: if user in line.split(): return True else: return False 

This is for a database.txt file made by a '' 'like' '' database with spaces between users. We need to extract a bit of raw data to give you a consistent answer.

If users are separated by special characters (quotes, periods, commas, etc.), I will use .replace("delimitingcharacter", " ") ..

 def CheckUserExists(user): with open("C:/~/database.txt", 'r') as file: for line in file: for word in line.split(): if user in word.replace(';', ' '): return True else: return False 
0
source

Source: https://habr.com/ru/post/976807/


All Articles