I am parsing strings containing code like the following. It can start with an empty lines followed by multiple optional patterns. These patterns can either be python-style inline comments (using a hash # character), or the command "!mycommand", and both must start at the beginning of a line. How can I write a regex matching up to the starting of the code?
mystring = """
# catch this comment
!mycommand
# catch this comment
#catch this comment too
!mycommand
# catch this comment
!mycommand
!mycommand
some code. match until the previous line
# do not catch this comment
!mycommand
# do not catch this comment
"""
import re
pattern = r'^\s*^#.*|!mycommand\s*'
m = re.search(pattern, mystring, re.MULTILINE)
mystring[m.start():m.end()]
mystring = 'code. do not match anything' + mystring
m = re.search(pattern, mystring, re.MULTILINE)
I want the regex to match the string up to "some code. catch until the previous line". I tried different things but I am probably stuck with the two multiple patterns