fork download
  1. #!/usr/bin/python
  2.  
  3. import re
  4.  
  5. all_lines = '#hello\n#monica, how re "u?\n#hello#robert\necho'
  6.  
  7. regex = re.compile(
  8. r'''\# # first hash
  9. .* # continues to (note: .* greedy)
  10. \# # last hash
  11. .*?$ # rest of the line. (note .*? non-greedy)
  12. ''',
  13. # Flags:
  14. # DOTALL: Make the '.' match any character at all, including a newline
  15. # VERBOSE: Allow comments in pattern
  16. # MULTILINE: Allow $ to match end of line
  17. re.DOTALL | re.VERBOSE | re.MULTILINE)
  18.  
  19. print re.search(regex, all_lines).group()
Success #stdin #stdout 0.01s 7852KB
stdin
Standard input is empty
stdout
#hello
#monica, how re "u?
#hello#robert