Hello I am trying to build a function that i can use in to search for an object or item from its name,
Here is what I have in sfind.py

def findobj(searchTxt,items):
    """
    This function will help you find an object or an item from list of
    items passed to it.
    """
    print items
    import re
    for each in items:
        print each
        result = re.search(searchTxt, each) #returns None if search fails
    if result:
        print each #and do whatever you need...
        return True
    else:
        print("Not found")

but every time i enter something to seach I get Not found.

try:
    reload(sfind)
    sfind.findobj("lambert1",['lambert1','lambert2','grey'])
except:
    import sfind
    sfind.findobj("lambert1",['lambert1','lambert2','grey'])

please help me, I guess something wqrong with the seach behaviour....

I do not understand so well but here version with everything I did not understand cut out

def findobj(searchTxt,items):
    """
    This function will help you find an object or an item from list of
    items passed to it.
    """
    for each in items:
        if each == searchTxt:
            print each #and do whatever you need...
            return True
        else:
            print("Not found")

print findobj("lambert1",['lambert1','lambert2','grey'])
print 'Using builtin', 'lambert1' in ['lambert1','lambert2','grey']

It's because you are only testing the search result on the last item of the sequence. Write if result: in the for loop.

if we use if each == searchTxt method then the search is performed for the exact match when I want to search for simmilar names as well...

Then

if searchTxt in each:

Should mean same as your wildcardless regular expression matching for example the in mathematics

>>> if re.search('the', 'mathematics'):
        print 'Yes'

Yes

which means i dont really need to built any new function if one already exist....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.