I always have a hard time understanding the logic of regex in python.
all_lines = '#hello\n#monica, how re "u?\n#hello#robert\necho\nfall and spring'
I want to retrieve the substring that STARTS WITH # until the FIRST \n THAT COMES RIGHT AFTER the LAST # - I.e., '#hello\n#monica, how re "u?\n#hello#robert'
So if I try:
>>> all_lines = '#hello\n#monica, how re "u?\n#hello#robert\necho'
>>> RE_HARD = re.compile(r'(^#.*\n)')
>>> mo = re.search(RE_HARD, all_lines)
>>> print mo.group(0)
#hello
Now, if I hardcode what comes after the first \n after the last #, i.e., I hardcode echo, I get:
>>> all_lines = '#hello\n#monica, how re "u?\n#hello#robert\necho'
>>> RE_HARD = re.compile(r'(^#.*echo)')
>>> mo = re.search(RE_HARD, all_lines)
>>> print mo.group(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
I get an error, no idea why. Seems the same as before.
This is still not want I want since in reality after the first \n that comes after the last # I may have any character/string...