Extract first_line_re function to encapsulate compatibility mechanism
Move import to the top
Re-arrange _first_line_re to put core functionality at the top level