<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def validate_regex(regex, sub_service):
 """Is the regex valid for StackInABox routing? :param regex: Python regex object to match the URI :param sub_service: boolean for whether or not the regex is for a sub-service :raises: InvalidRouteRegexError if the regex does not meet the requirements. """

        # The regex generated by stackinabox starts with ^
        # and ends with $. Enforce that the provided regex does the same.

        if regex.pattern.startswith('^') is False:
            logger.debug('StackInABoxService: Pattern must start with ^')
            raise InvalidRouteRegexError('Pattern must start with ^')

        # Note: pattern may end with $ even if sub_service is True
        if regex.pattern.endswith('$') is False and sub_service is False:
            logger.debug('StackInABoxService: Pattern must end with $')
            raise InvalidRouteRegexError('Pattern must end with $')

        # Enforce that if the pattern does not end with $ that it is a service
        if regex.pattern.endswith('$') is True and sub_service is True:
            logger.debug(
                'StackInABoxService: Sub-Service RegEx Pattern must not '
                'end with $')
            raise InvalidRouteRegexError('Pattern must end with $')