-3

I have a file with sections for different environments as follows:

#developmentsectionstart
  # development environment only
  context 'environment => development' do
    let(:trusted_facts) do
      super().merge('pp_apptier' => 'development')
    end
#developmentsectionend

#trainingsectionstart
  # training environment only
  context 'environment => training' do
    let(:trusted_facts) do
      super().merge('pp_apptier' => 'training')
    end
#trainingsectionend

I need to be able to delete sections based on environments entered by the user which I put in a list. e.g. envlist is the list entered by user and allenvlist are all the environments in the file.

I need to remove all sections not entered by the user e.g. test, stress & training i.e. everything from #trainingsectionstart to #trainingsectionend etc. I tried passing the environments entered by the user as a variable into re.sub. Not working.

envlist = ['development','qa','production']
allenvlist = ['development','test','qa','stress','training','production']

new_env_list =[]
for element in allenvlist:
    if element not in envlist:
    new_env_list.append(element)
for i in range(0,len(new_env_list)):
    sectionstart = '#'+new_env_list[i]+'start'
    sectionend = '#'+new_env_list[i]+'end'
    newdata = re.sub(r'sectionstart.*?sectionend','', filedata, flags=re.DOTALL)
martineau
  • 119,623
  • 25
  • 170
  • 301
abrahavt
  • 45
  • 6

1 Answers1

-2

Variables aren't expanded in strings.

newdata = re.sub(sectionstart + '.*?' + sectionend,'', filedata, flags=re.DOTALL)

If the variables could potentially contain special regexp characters and you want them treated literally, you should use re.escape() to protect them.

newdata = re.sub(re.escape(sectionstart) + '.*?' + re.escape(sectionend),'', filedata, flags=re.DOTALL)
Barmar
  • 741,623
  • 53
  • 500
  • 612