dropdown menu

Back To Home

Thursday, November 14, 2019

python- match word in a file not single character

Match whole word albert or robert not al or b and etc.

Python code:

import re

with open('line.txt', 'r') as fh:
    contents = fh.read()

loop = True
while loop:
    user_input = input('Enter a name: ').strip()
    if (re.search(r'\b'+ re.escape(user_input) + r'\b', contents, re.MULTILINE)):
        print("That name exists!")
    else:

        print("Couldn't find the name.")


line.txt
albert
robert
                                              
                                                               

No comments:

Post a Comment