Regex with python | SoloLearn: Learn to code for FREE!


Regex with python

Hi people! Can anyone tell me how would I use regex in python. For exemple I want to check if a word is in a dictionary that has a word on every line. If ‘hone’ in open(‘dictionary.txt’).read(): Print(‘found!’) This would print ‘found!’ even if it finds word ‘phone’ or ‘honey’ in dictionary As a linux guy I know how it would be done with grep and regex grep ‘^word$’ dictionary.txt How can I do that in python? Thanks!

3/23/2018 4:06:55 PM

Višeslav Dasović

6 Answers

New Answer


I wrote a code on some RegEx examples quite a time ago. Perhaps you'll make a use of it.


If you have a string that looks like: st = “phone\nhoney” You can do: lis = st.split(“\n”) It returns a list of strings: “phone” and “honey”. Then you can search the list, and you won’t have that problem.



here is an example code:


Lets say I have a string ‘one’ and I want to check if that string is in a dictionary. But I want it to follow these rules when checking: A line must start with that string A line must end with that string Correct: (Start of line)one(end of line) Wrong: (Start of line)phone(end of line In both these cases the example i have gave you will find it


😁😁😁 Thanks man! Exectly what I was looking for!!!