import re


def validate_cron_string(cron_string, error_on_invalid=False):
	"""
	Validate that a string is a Unix cron string.
	"""

	# Note: This is also a Temporal function, but I'm trying to avoid making Temporal a dependency of BTU.
	crontab_time_format_regex = re.compile(
		r"{0}\s+{1}\s+{2}\s+{3}\s+{4}".format(
			r"(?P<minute>\*(\/[0-5]?\d)?|[0-5]?\d)",
			r"(?P<hour>\*|[01]?\d|2[0-3])",
			r"(?P<day>\*|0?[1-9]|[12]\d|3[01])",
			r"(?P<month>\*|0?[1-9]|1[012])",
			r"(?P<day_of_week>\*|[0-6](\-[0-6])?)")  # end of str.format()
	)  # end of re.compile()

	if crontab_time_format_regex.match(cron_string) is None:
		if error_on_invalid:
			raise Exception(f"String '{cron_string}' is not a valid Unix cron string.")
		return False
	return True