0

I currently have a config file which has words starting in as "ltm AZ2b, then followed by a few lines of config and then finally followed by vs-index as the end of the line.

Here's an example:


ltm virtual ync-Prod-Environment-VS { ..... ..... } ... vs-index 56

--right under this is another config set:

ltm virtual ync-Prod-Environment-VS-AZ2b { ..... ..... } ... vs-index 45


As you can see above(highlighted as well), there are multiple sets of config, and each set starts with ,-VS (multiple lines of config under this) and another set of similar config with the same name but with AZ2b, like: -VS-AZ2b (multiple lines of config under this as well)

I'm trying to extract only the config set which starts with -VS-AZ2b and ends with its vs-index.

I've written the following code but it is not working very well:


import re

fhandle=open('C:/Python Programs/Active.txt', 'r') str=fhandle.read()

y=re.findall('^.+AZ2b .+? vs-index [0-9].', str, re.DOTALL)

for line in y: line = line.replace('\n', "\n") print line


The output that i get is everything - including the config set with -VS. Not sure why. Can someone help?

Just to check, i took a file and added only the config sets with -VS and the program did not return anything. So i guess the code is only partially correct.

Thanks!

rubrick
  • 1
  • 2

1 Answers1

0

Actually i got it to work. I had to modify the regex this way:

y=re.findall('^.+AZ2b [\s\S]*?.+vs-index [0-9].\n}',str, re.MULTILINE)

The [\s\S]*? was missing in the regex. I got this info from here in stackflow:

Also re.DOTALL was not working very well for this as it was matching everything, so i had to use re.MULTILINE instead

Can I match multiline string in python without using re.DOTALL?

Thanks all!!

rubrick
  • 1
  • 2