Searches for a pattern that matches the given filename.

        :return A matching pattern or None if there is no matching pattern.

def match(self, filename):
        """Searches for a pattern that matches the given filename.

        :return A matching pattern or None if there is no matching pattern.
        """

        try:
            for regex, patterns in self._regex_patterns:
                match = regex.match(filename)

                debug_template = "%s against %s: %%s" % (filename, regex._real_regex.pattern)

                if match:
                    if self.debug:
                        logger.info(debug_template % "hit")
                    return patterns[match.lastindex -1]

                if self.debug:
                    logger.info(debug_template % "miss")

        except Exception as e:
            # We can't show the default e.msg to the user as thats for
            # the combined pattern we sent to regex. Instead we indicate to
            # the user that an ignore file needs fixing.
            logger.error('Invalid pattern found in regex: %s.', e.msg)
            e.msg = "File ~/.bazaar/ignore or .bzrignore contains error(s)."
            bad_patterns = ''
            for _, patterns in self._regex_patterns:
                for p in patterns:
                    if not Globster.is_pattern_valid(p):
                        bad_patterns += ('\n  %s' % p)
            e.msg += bad_patterns
            raise e
        

        return None