Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back references in re.py #220

Open
thabubble opened this issue Oct 20, 2017 · 2 comments
Open

Back references in re.py #220

thabubble opened this issue Oct 20, 2017 · 2 comments

Comments

@thabubble
Copy link

Hello. I wrote something to make back references(\0-\9) work with re.sub. Not the prettiest but it does seem to work. Unfortunately, I did not understand how to commit this properly. So I'm leaving it here for now. Sorry for the inconvenience.

@thabubble
Copy link
Author

thabubble commented Oct 20, 2017

I seem to have some problems with github in my browser. I can't edit or upload files, etc.

--- .micropython/lib/re.py.bak
+++ .micropython/lib/re.py
@@ -85,7 +85,21 @@
 
     def sub(self, repl, s, count=0):
         if not callable(repl):
-            assert "\\" not in repl, "Backrefs not implemented"
+            replb = []
+            if '\\' in repl:
+                n = len(repl) - 1
+                l = f = 0
+                while True:
+                    f = repl.find('\\', f)
+                    if f == -1 or f == n:
+                        break
+                    c = repl[f+1]
+                    if c.isdigit():
+                        replb.append((int(c), l, f))
+                        l = f = f + 2
+                    else:
+                        f += 2
+                replb.append((None, l, None))
         res = ""
         while s:
             m = self.search(s)
@@ -95,6 +109,11 @@
             res += s[:beg]
             if callable(repl):
                 res += repl(m)
+            elif replb:
+                for g in replb:
+                    res += repl[g[1]:g[2]]
+                    if g[0] is not None:
+                        res += m.group(g[0])
             else:
                 res += repl
             s = s[end:]

@xrmx
Copy link
Contributor

xrmx commented Nov 11, 2017

@thabubble you have to fork the repo, commit your changes locally with git (while at it you want to add tests for the featured you added) and then push them to your repo on github. Then you need to open a pull request to this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants