fork download
  1. import re
  2.  
  3. string = """
  4. BeginA
  5. Qwerty
  6. Element 11 35
  7. EndA
  8.  
  9. BeginB
  10. Element 12 38
  11. ...
  12. Element 198 38
  13. EndB
  14.  
  15. BeginA
  16. Element 81132 38
  17. SomethingElse
  18. EndA
  19.  
  20. BeginB
  21. Element 12 39
  22. Element 198 38
  23. EndB"""
  24.  
  25.  
  26. block = re.compile(r'BeginB[\s\S]+?EndB')
  27. element = re.compile(r'Element.*?\b38\b')
  28.  
  29. def repl(match):
  30. return element.sub('Element ABC', match.group(0))
  31.  
  32. nstring = block.sub(repl, string)
  33. print(nstring)
Success #stdin #stdout 0.02s 27712KB
stdin
Standard input is empty
stdout
BeginA
Qwerty
Element 11 35
EndA

BeginB
Element ABC
...
Element ABC
EndB

BeginA
Element 81132 38
SomethingElse
EndA

BeginB
Element 12 39
Element ABC
EndB