def test_groupdict(self): m = rure.match(u"(?P<first_name>\\w+) (?P<last_name>\\w+)", u"Malcolm Reynolds") self.assertEqual(m.groupdict(), { u'first_name': u'Malcolm', u'last_name': u'Reynolds' })
def is_constant_math(arg): """ See if a given expression is a simple math expression with all literal numbers. """ # Sanity check. If there are variables in the expression it is not all literals. if (isinstance(arg, VBA_Object)): var_visitor = var_in_expr_visitor() arg.accept(var_visitor) if (len(var_visitor.variables) > 0): return False # Speed this up with the rure regex library if it is installed. try: import rure as local_re except ImportError: import re as local_re base_pat = "(?:\\s*\\d+(?:\\.\\d+)?\\s*[+\\-\\*/]\\s*)*\\s*\\d+" paren_pat = base_pat + "|(?:\\((?:\\s*" + base_pat + "\\s*[+\\-\\*\\\\]\\s*)*\\s*" + base_pat + "\\))" arg_str = str(arg).strip() try: arg_str = unicode(arg_str) except UnicodeDecodeError: arg_str = filter(isprint, arg_str) arg_str = unicode(arg_str) return (local_re.match(unicode(paren_pat), arg_str) is not None)
def test_complicated_group(self): m = rure.match(u"(?P<first_name>\\w+) (?P<last_name>\\w+)", u"Malcolm Reynolds") self.assertEqual(m.group(u'first_name'), u'Malcolm') self.assertEqual(m.group(u'last_name'), u'Reynolds') self.assertEqual(m.group(1), u'Malcolm') self.assertEqual(m.group(2), u'Reynolds')
def is_constant_math(arg): """See if a given expression is a simple math expression with all literal numbers. @param arg (VBA_Object object) The expression to check. @return (boolean) True if this is a simple math expression with all numeric literals, False if not. """ # Sanity check. If there are variables in the expression it is not all literals. if (isinstance(arg, VBA_Object)): var_visitor = var_in_expr_visitor() arg.accept(var_visitor) if (len(var_visitor.variables) > 0): return False # Some things are not math expressions. if (isinstance(arg, dict) or contains_excel(arg)): return False # Speed this up with the rure regex library if it is installed. try: import rure as local_re except ImportError: # Renaming of failed to import rure package. # pylint: disable=reimported import re as local_re # Use a regex to see if this is an all constant expression. base_pat = "(?:\\s*\\d+(?:\\.\\d+)?\\s*[+\\-\\*/]\\s*)*\\s*\\d+" paren_pat = base_pat + "|(?:\\((?:\\s*" + base_pat + "\\s*[+\\-\\*\\\\]\\s*)*\\s*" + base_pat + "\\))" arg_str = safe_str_convert(arg).strip() try: arg_str = unicode(arg_str) except UnicodeDecodeError: arg_str = filter(isprint, arg_str) arg_str = unicode(arg_str) return (local_re.match(unicode(paren_pat), arg_str) is not None)
def test_groups_optional(self): m = rure.match(u"(\\d+)\\.?(\\d+)?", u"24") self.assertEqual(m.groups(), (u'24', None)) self.assertEqual(m.groups(u'0'), (u'24', u'0'))
def test_groups(self): m = rure.match(u"(\\d+)\\.(\\d+)", u"24.1632") self.assertEqual(m.groups(), ('24', '1632'))
def test_group_last_match(self): m = rure.match(u"(..)+", u"a1b2c3") self.assertEqual(m.group(1), u'c3')
def test_group(self): m = rure.match(u"(\\w+) (\\w+)", u"Isaac Newton, physicist") self.assertEqual(m.group(0), u'Isaac Newton') self.assertEqual(m.group(1), u'Isaac') self.assertEqual(m.group(2), u'Newton') self.assertEqual(m.group(1, 2), ('Isaac', 'Newton'))