Commits

Olemis Lang committed 879f0f3

Brython #26 : MatchObject.group handling integer indices . Return tuples . Test cases in Python 3 doc

Comments (0)

Files changed (1)

t26/t26_r4181bd84a226_re.match.diff

 
 diff -r 4181bd84a226 site/tests/index.html
 --- a/site/tests/index.html	Sat Apr 06 12:04:19 2013 -0500
-+++ b/site/tests/index.html	Sun Apr 07 00:23:41 2013 -0500
++++ b/site/tests/index.html	Sun Apr 07 01:05:59 2013 -0500
 @@ -84,20 +84,27 @@
  
  Test suite:
    <div id="editor"></div>
 diff -r 4181bd84a226 site/tests/test_re.py
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/site/tests/test_re.py	Sun Apr 07 00:23:41 2013 -0500
-@@ -0,0 +1,17 @@
++++ b/site/tests/test_re.py	Sun Apr 07 01:05:59 2013 -0500
+@@ -0,0 +1,32 @@
 +import re
 +
 +m = re.search('world', 'hello world')
 +assert m is not None
 +assert m.string == 'hello world'
-+assert m.groups() == []
++assert m.groups() == ()
 +
 +m = re.match('world', 'hello world')
 +assert m is None
 +m = re.match('hello', 'hello world')
 +assert m is not None
 +assert m.string == 'hello world'
-+assert m.groups() == []
++assert m.groups() == ()
++
++# Samples code in Python 3 doc
++
++m = re.match(r"(\d+)\.(\d+)", "24.1632")
++assert m.groups() == ('24', '1632')
++
++m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
++assert m.group(0) == 'Isaac Newton'
++assert m.group(1) == 'Isaac'
++assert m.group(2) == 'Newton'
++assert m.group(1, 2) == ('Isaac', 'Newton')
++
++m = re.match(r"(..)+", "a1b2c3")
++assert m.group(0) == 'a1b2c3'
++assert m.group(1) == 'c3'
 +
 +print('all tests ok..')
 +
 diff -r 4181bd84a226 src/Lib/re.py
 --- a/src/Lib/re.py	Sat Apr 06 12:04:19 2013 -0500
-+++ b/src/Lib/re.py	Sun Apr 07 00:23:41 2013 -0500
-@@ -2,20 +2,29 @@
++++ b/src/Lib/re.py	Sun Apr 07 01:05:59 2013 -0500
+@@ -2,20 +2,38 @@
  
  class MatchObject:
  
 -        self.mo = mo
 +    def __init__(self,jsmatch):
 +        self._jsmatch = jsmatch
++
++    def group(self, *args):
++        # FIXME: Support for group names ?
++        if len(args) == 0:
++            return self._jsmatch[0]
++        elif len(args) == 1:
++            return self._jsmatch[args[0]]
++        else:
++            return tuple(self._jsmatch[x] for x in args)
  
      def groups(self):
 -        return self.mo[1:]
-+        return self._jsmatch[1:]
++        return tuple(self._jsmatch[1:])
  
  def search(pattern,src,flags=None):
 +    # FIXME : Check whether `g` is appropriate as this is used for match too