Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. s = '(x + 2 - y)'
  2.  
  3. '(x + 2 - y)'
  4.  
  5. pattern = '(?P<char>[(+)])'
  6. repl = '\g<char>'
  7.  
  8. >>> s = '(x + 2 - y)'
  9. >>> pattern = '(?P<char>[(+)])'
  10. >>> repl = '\g<char>'
  11. >>>
  12. >>> re.sub(pattern, repl, s)
  13. '\g<char>x \g<char> 2 - y\g<char>'
  14. >>>
  15.  
  16. >>> repl = '\\g<char>'
  17. >>> re.sub(pattern, repl, s)
  18. '\g<char>x \g<char> 2 - y\g<char>'
  19.  
  20. >>> repl = r'\\g<char>'
  21. >>> re.sub(pattern, repl, s)
  22. '\\g<char>x \\g<char> 2 - y\\g<char>'
  23.  
  24. >>> repl = r'\g<char>'
  25. >>> re.sub(pattern, repl, s)
  26. '\g<char>x \g<char> 2 - y\g<char>'
  27.  
  28. >>> repl = '\g<char>'
  29. >>> re.sub(pattern, repl, s)
  30. '\g<char>x \g<char> 2 - y\g<char>'
  31.  
  32. >>> repl = '\1'
  33. >>> re.sub(pattern, repl, s)
  34. '\x01x \x01 2 - y\x01'
  35.  
  36. >>> repl = '\g<1>1'
  37. >>> re.sub(pattern, repl, s)
  38. '\g<1>1x \g<1>1 2 - y\g<1>1'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement