0

I've a code below to find a match keyword in a string. My question is how can I include multiple keywords in "find"? Should I use array or something.

What is the best way to handle multiple keyword that we would like to search. Assume all keyword must be matched, then result is PASS.

Tried this. Failed..

("\bpython\b", "\bgreat\b")?? Any ideas?

import re

string = "python is great!"
find = ("\\bpython\\b")


if re.search(find, string):
    print ("found")
else:
    print ("not found")
Izzat Z.
  • 437
  • 1
  • 7
  • 15

2 Answers2

2

To match both, you either need two regular expressions or need to specify all orders:

find = r'\bpython\b.*\bgreat\b|\bgreat\b.*\bpython\b|'

For more keywords, I recommend you to use multiple regular expressions in a loop:

import re

string = "python is great!"
words=["python","is","great"]

all_found=True
for w in words:
 if not re.search(r"\b"+w+r"\b", string):
    all_found=False
    break

if all_found:
    print ("found all words")
else:
    print ("at least one word missing").
jofel
  • 3,297
  • 17
  • 31
  • I guess you wanted to use `r"\b"+w+r"\b"`. I think this should work better than `r"(?s)^(?=.*\b(?:python|great)\b)"`. – Wiktor Stribiżew Jan 08 '16 at 08:55
  • Instead of using `all_found=True`, you can put the lines into a function that returns `False` if the regex doesn't match in any of the loop iterations, `True` otherwise. – Jan Eglinger Jan 08 '16 at 09:03
  • @JanEglinger Thanks for the suggestion. This would be more beautiful. But the code size would be more or less the same and the runtime 3x slower (at least for the example I tried)... – jofel Jan 08 '16 at 09:12
1

You can use a loop to match all the words in find. Loop through, and when it cannot find a word break the loop (and print the word that is missing). If all words are found, print 'all substrings are found'.

import re

string = r"python is great!"
find = [r'python',r'great']

for substring in find:  
    if not re.search(r'\b'+substring+r'\b', string):
        print("'%s' not found" % substring)
        break
else:
    print ("all substrings are found")
Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29