Regex with python | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

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!

23rd Mar 2018, 4:06 PM
ViÅ”eslav Dasović
ViÅ”eslav Dasović - avatar
6 Respostas
+ 6
I wrote a code on some RegEx examples quite a time ago. Perhaps you'll make a use of it. https://code.sololearn.com/c40z8B7tOdCN/?ref=app
23rd Mar 2018, 7:42 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
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.
23rd Mar 2018, 4:42 PM
Pedro Demingos
Pedro Demingos - avatar
23rd Mar 2018, 5:43 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
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
23rd Mar 2018, 4:49 PM
ViÅ”eslav Dasović
ViÅ”eslav Dasović - avatar
0
šŸ˜šŸ˜šŸ˜ Thanks man! Exectly what I was looking for!!!
23rd Mar 2018, 5:45 PM
ViÅ”eslav Dasović
ViÅ”eslav Dasović - avatar