Find a word in a string python using regex or other methods
Question: I am trying to go through an array of words and check if they exist in a
string. I understand there are many options for doing this such as using
re.search but I need to differ between some words (ie. Java vs Javascript)

An example:

    
    
    import re
    
    s = 'Some types (python, c++, java, javascript) are examples of programming.'
    words = ['python', 'java', 'c++', 'javascript', 'programming']
    
    for w in words:
        p = re.search(w, s)
        print(p)
    
    >><_sre.SRE_Match object; span=(12, 18), match='python'>
    >><_sre.SRE_Match object; span=(20, 24), match='java'>
    >><_sre.SRE_Match object; span=(20, 30), match='javascript'>
    >><_sre.SRE_Match object; span=(48, 59), match='programming'>
    

The above works to an extent but matches Java with Javascript.

EDIT: Here was my solution

    
    
    for w in words:
            regexPart1 = r"\s"
            regexPart2 = r"(?:!+|,|\.|\·|;|:|\(|\)|\"|\?+)?\s"
            p = re.compile(regexPart1 + re.escape(w) + regexPart2 , re.IGNORECASE)
            result = p.search(s)
    


Answer: You want to add word boundary marks to you regular expressions, say
`r'/bjavascript/b'` in place of merely `'javascript'`. (Note also that `+`
should be escaped in `c++` )

Also, iteration over words to match lacks potential efficiency of a compiled
regexp. It may be better to combine the regexps into one:

    
    
    w = r'\b(?:python|java|c\+\+|javascript|programming)\b'
    re.search(w,s)
    

