Advertisement
eacousineau

Bug in Python re.sub() with re.MULTILINE?

Aug 22nd, 2012
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. >> import sys, os
  2. >>> print sys.version
  3. 2.7.3 (default, Aug  1 2012, 05:14:39)
  4. [GCC 4.6.3]
  5. >>> print [os.uname()[i] for i in [0, 2, 3, 4]]
  6. ['Linux', '3.2.0-25-generic', '#40-Ubuntu SMP Wed May 23 20:30:51 UTC 2012', 'x86_64']
  7. >>> import re
  8. >>> pattern = r'int (\w+) = (.*?);'
  9. >>> replace = r'\t\1 = \2,'
  10. >>> text = 'int x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint y = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;\nint x = 5;'
  11. >>> print text, "\n\n"
  12. int x = 5;
  13. int x = 5;
  14. int x = 5;
  15. int x = 5;
  16. int y = 5;
  17. int x = 5;
  18. int x = 5;
  19. int x = 5;
  20. int x = 5;
  21. int x = 5;
  22. int x = 5;
  23. int x = 5;
  24. int x = 5;
  25. int x = 5;
  26. int x = 5;
  27. int x = 5;
  28. int x = 5;
  29.  
  30.  
  31. >>> print re.sub(pattern, replace, text, re.MULTILINE), "\n\n"
  32.     x = 5,
  33.     x = 5,
  34.     x = 5,
  35.     x = 5,
  36.     y = 5,
  37.     x = 5,
  38.     x = 5,
  39.     x = 5,
  40. int x = 5;
  41. int x = 5;
  42. int x = 5;
  43. int x = 5;
  44. int x = 5;
  45. int x = 5;
  46. int x = 5;
  47. int x = 5;
  48. int x = 5;
  49.  
  50.  
  51. >>> print re.sub(pattern, replace, text), "\n\n"
  52.     x = 5,
  53.     x = 5,
  54.     x = 5,
  55.     x = 5,
  56.     y = 5,
  57.     x = 5,
  58.     x = 5,
  59.     x = 5,
  60.     x = 5,
  61.     x = 5,
  62.     x = 5,
  63.     x = 5,
  64.     x = 5,
  65.     x = 5,
  66.     x = 5,
  67.     x = 5,
  68.     x = 5,
  69.  
  70.  
  71. >>> print re.subn(pattern, replace, text, 0, re.MULTILINE)[0], "\n\n"
  72.     x = 5,
  73.     x = 5,
  74.     x = 5,
  75.     x = 5,
  76.     y = 5,
  77.     x = 5,
  78.     x = 5,
  79.     x = 5,
  80.     x = 5,
  81.     x = 5,
  82.     x = 5,
  83.     x = 5,
  84.     x = 5,
  85.     x = 5,
  86.     x = 5,
  87.     x = 5,
  88.     x = 5,
  89.  
  90.  
  91. >>> regex = re.compile(pattern, re.MULTILINE)
  92. >>> print regex.sub(replace, text)
  93.     x = 5,
  94.     x = 5,
  95.     x = 5,
  96.     x = 5,
  97.     y = 5,
  98.     x = 5,
  99.     x = 5,
  100.     x = 5,
  101.     x = 5,
  102.     x = 5,
  103.     x = 5,
  104.     x = 5,
  105.     x = 5,
  106.     x = 5,
  107.     x = 5,
  108.     x = 5,
  109.     x = 5,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement