how can i search for a specific word in python?

后端 未结 1 1838
萌比男神i
萌比男神i 2021-01-25 12:24

Is there a way to search a specific word in python, like \'or\'. I did this:

word = raw_input(\"what do you want to search? \")
for filee in open(\'text.txt\'):         


        
相关标签:
1条回答
  • 2021-01-25 13:15

    Use regex with word boundaries:

    >>> import re
    >>> r = re.compile(r'\bor\b')
    >>> r.search('and or not')
    <_sre.SRE_Match object at 0x7f2f7f8b2ed0>
    >>> r.search('and xor not')
    
    0 讨论(0)
提交回复
热议问题