i have python input list containing both strings , regexs.
str_regex = ['normal_string_1', ''^(?![_\-])[a-za-z0-9\-_]+$', 'normal_string_2']
i need segregate list list of strings , list of regexs. in summary below input , required output.
input :
['normal_string_1', ''^(?![_\-])[a-za-z0-9\-_]+$', 'normal_string_2']
output :
['normal_string_1', 'normal_string_2'], ['^(?![_\-])[a-za-z0-9\-_]+$']
i planning form regex this. "should contains [a-z]
, [a-z]
". best way it?
if understand problem correctly, want pass string function along list of different regexes, returning regex matches. should trick:
import re def contains(string, regexes): lst = [] regex in regexes: match = re.findall(regex, string) if len(match) > 0 , len(match[0]) == len(string): return string + " contains \"" + regex + "\"" return "no matching regex found" if __name__ == "__main__": regexes = [ "[a-z]+", # lower case "[a-z]+", # upper case "[a-za-z]+", # mixed case "\d+", # digits "[a-z\d]+", # digits , lower case "[a-z\d]+", # digits , upper case "[a-za-z\d]+" # digits , mixed case ] print contains("should" , regexes) print contains("should" , regexes) print contains("should" , regexes) print contains("123" , regexes) print contains("123as" , regexes) print contains("123as" , regexes) print contains("123as" , regexes)
the above prints
should contains "[a-z]+" should contains "[a-z]+" should contains "[a-za-z]+" 123 contains "\d+" 123as contains "[a-z\d]+" 123as contains "[a-z\d]+" 123as contains "[a-za-z\d]+"
Comments
Post a Comment