Member Avatar for cyon

Can someone help me resolve the following error or point me in the right direction? An incomplete value is returned if ' is in the list value.

Thanks for any help.

def FindName(self,name,list1):
		""" searches for a patten in the list, returns the complete list entry. Is case sensitive.
		May return incomplete value if ' is in the list value (ie confucius's)"""
		if name in list1:
			return name
		else:
			string1 = str(list1).replace('"',"'")
			pattern = re.compile("[']"+name+"[^']*")
			match = re.search(pattern,string1)
			if match:
				if match.group().lstrip("'") in list1:
					return match.group().lstrip("'")
				else:
					print 'Fail %s' %match.group().lstrip("'")
					return None
			else:
				return None

I spotted that it has program, but I do not understand the re module enough to tell how to use it. Here is my test for others to check easier.

import re
def FindName(name,list1):
    """ searches for a pattern in the list, returns the complete list entry. Is case sensitive.
    May return incomplete value if ' is in the list value (ie confucius's)"""
    if name in list1:
        return name
    else:
        string1 = str(list1).replace('"',"'")
        pattern = re.compile("[']"+name+"[^']*")
        match = re.search(pattern,string1)
        if match:
            if match.group().lstrip("'") in list1:
                return match.group().lstrip("'")
            else:
                print 'Fail %s' %match.group().lstrip("'")
                return None
        else:
            return None
china=['In', 'China', 'people', 'teach',"Confusius's",
       'teachings','dilligently.','Those',"Confusius''s",
       'teachings','are','called','Taoism.']

this='Conf'
print FindName(this,china)

print [(i,x) for (i,x) in enumerate(china) if x.startswith(this)]

""" Output:
Fail Confusius
None
[(4, "Confusius's"), (8, "Confusius''s")]
"""

Thanks tonyjv for the clarification and the test data.

#!/usr/bin/env python
import re
def FindName(name,list1):
    """ searches for a pattern in the list, returns the complete list entry(s)
    as a string. Is case sensitive.
    """
    mlist = []
    pattern = r"""^(%s.*)$""" % (name)
    for item in list1:
        match = re.search(pattern, item)
        if match:
            mlist.append(match.group())
        
    if len(mlist) > 0:
        return ', '.join(mlist)
    else:
        return 'Fail ' + name

            
china=['In', 'China', 'people', 'teach',"Confusius's",
       'teachings','dilligently.','Those',"Confusius''s",
       'teachings','are','called','Taoism.']

this='Conf'

print FindName(this,china)

print [(i,x) for (i,x) in enumerate(china) if x.startswith(this)]

"""Output:
Confusius's, Confusius''s
[(4, "Confusius's"), (8, "Confusius''s")]
"""
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.