import re


def rule_to_regexp(rule):
	""" returns a tuple (rule_name, rule_regexp) e.g. ("keep", <regexp for matching product names>  """
	#this part might be moved out and regular expression cached
	(rule_name, rule_test) = rule.split()
	
	# we create a regexp out of rule
	
	#we ignore the 4th rule:
	rule_parts =rule_test.split("_")
	if len(rule_parts) == 4 :
		# we replace the last one to asterix
		rule_parts[3] = "*" 
	rule_test = "_".join(rule_parts) 
	
	# make a regexp
	rule_test = rule_test.replace("*", ".*")

	rule_regexp = re.compile("^"+rule_test+"$")
	
	return (rule_name, rule_regexp)