<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def grep(expression, file, flags=0, invert=False):
 	""" 	Search a file and return a list of all lines that match a regular expression. 	:param str expression: The regex to search for. 	:param file: The file to search in. 	:type file: str, file 	:param int flags: The regex flags to use when searching. 	:param bool invert: Select non matching lines instead. 	:return: All the matching lines. 	:rtype: list 	"""

	# requirements = re
	if isinstance(file, str):
		file = open(file)
	lines = []
	for line in file:
		if bool(re.search(expression, line, flags=flags)) ^ invert:
			lines.append(line)
	return lines