def get_regex_name(self, name): """ If a regex is used within `name`, returns a compiled regex that is able to find all units with a name that matches some pattern. Returns `None` otherwise. """ if (name.startswith(REGEX_SYM) and (name.endswith(REGEX_SYM) or name[-2] == REGEX_SYM or name[-3] == REGEX_SYM)): splitted_str = [e for e in name.split(REGEX_SYM) if e != ""] if len(splitted_str) == 1: return re.compile(name[1:-1].replace('\\' + REGEX_SYM, REGEX_SYM)) flags = splitted_str[-1] text_without_flags = rchop(name, flags) self._is_regex_global = 'g' in flags if 'i' in flags: return re.compile( text_without_flags[1:-1].replace('\\' + REGEX_SYM, REGEX_SYM), re.IGNORECASE) return re.compile(text_without_flags[1:-1].replace( '\\' + REGEX_SYM, REGEX_SYM)) return None
def test_not_ending(self): assert rchop("this is a test", "nothing") == "this is a test" assert rchop("Hello", "hello") == "Hello"
def test_ending(self): assert rchop("this is a test", "test") == "this is a " assert rchop("another example", "ample") == "another ex"