fork download
  1. import re
  2.  
  3. pattern="^(?:\S.*\n)*.*((?<=^abc )\d+|x(?==\d+$))"
  4. s = ("x=0\n"
  5. "abc 20 \n"
  6. "def 76 \n"
  7. "ghi 11\n\n"
  8. "x=1\n"
  9. "def 45\n"
  10. "ghi 32\n\n"
  11. "x=2\n"
  12. "jkl 56\n"
  13. "mno 76\n"
  14. "abc 134\n\n"
  15. ".\n"
  16. ".\n"
  17. ".\n\n"
  18. "x=393\n"
  19. "xyz 21\n"
  20. "abc 9\n\n"
  21. "x=394\n"
  22. "ghi\n"
  23. "def\n"
  24. "xyz")
  25.  
  26. res=re.findall(pattern, s, re.M)
  27. print(res)
Success #stdin #stdout 0.03s 9580KB
stdin
Standard input is empty
stdout
['20', 'x', '134', '9', 'x']