def extract_regex(regex, text, encoding='utf-8'):
 if isinstance(regex, basestring):
  regex = re.compile(regex)
 try:
  strings = [regex.search(text).group('extract')]
 except:
  strings = regex.findall(text)
 strings = flatten(strings)
 if isinstance(text, unicode):
  return [remove_entities(s, keep=['lt', 'amp']) for s in strings]
 else:
  return [remove_entities(unicode(s, encoding), keep=['lt', 'amp']) for s in strings]

def extract_regex ( regex , text , encoding = 'utf-8' ) : if isinstance ( regex , basestring ) : regex = re . compile ( regex ) try : strings = [ regex . search ( text ) . group ( 'extract' ) ] except : strings = regex . findall ( text ) strings = flatten ( strings ) if isinstance ( text , unicode ) : return [ remove_entities ( s , keep = [ 'lt' , 'amp' ] ) for s in strings ] else : return [ remove_entities ( unicode ( s , encoding ) , keep = [ 'lt' , 'amp' ] ) for s in strings ]
extract a list of unicode strings from the given text/encoding using the following policies: * if the regex contains a named group called "extract" that will be returned * if the regex contains multiple numbered groups .