code: def match_validator(expression):
      
      if isinstance(expression, str):
          compiled = re.compile(expression)
      elif hasattr(expression, 'match'):
          # check it early so we could say something is wrong early
          compiled = expression
      else:
          raise TypeError(
              'Provided match is nor a string nor has a match method '
              '(like re expressions)'
          )
  
      def validator(value):
          if not compiled.match(value):
              # note: make it a list for consistent representation
              raise ValidationError(
                  "{} does not match pattern: {}".format(
                      value,
                      compiled.pattern
                      if hasattr(compiled, 'pattern')
                      else compiled
                  )
              )
  
      return validator
docs: Return validator function that will check if matches given expression.
  
      Args:
          match: if string then this will be converted to regular expression
             using ``re.compile``. Can be also any object that has ``match()``
             method like already compiled regular regular expression or custom
             matching object/class.