import re


def collect(filename, regex = None, negregex = None, start = [], end = []):
  """ Collect the lines from a file into an array, filtering the results
   
      filename  -  the filename to collect lines from
      regex     -  collect lines that only match the given expresssion
      negregex  -  exclude lines that match the given expression
      start     -  additional elements at the start
      end       -  additional elements at the end
  """
  out = start
  with open(filename) as infile:
    for line in infile:
      if not regex or re.search(regex, line):
        if not negregex or not re.search(negregex, line):
          out.append(line[:-1])

  for line in end:
    out.append(line)

  return out