1

I would like get all data between two strings

I want all text from 'config firewall policy' to 'end' (From 'Configure Firewall Policy' to the first line starting with 'End' )

Sample of initial text:

config firewall policy
    edit 1
        set name "demo"
        set uuid demo
        set srcintf "demo-b-demo"
        set dstintf "demo"
        set srcaddr "demo.demo.demo.110" "host_10.demo.demo.129"
    next
    edit 2
        set name "demo rule"
        set uuid demo-c532-51eb-end-demo
        set srcintf "end-demo-demo"
        set dstintf "demo"
        set srcaddr "demo.40.demo.129" "demo.40.demo.110"
    next
end
config firewall policy
    edit 3
        set name "VIP-demo"
        set uuid demo-d428-end-demo-demo
        set srcintf "demo-b-inside"
        set dstintf "demo"
        set srcaddr "demo.demo.128.demo" "demo.demo.128.demo"
    next
end

Result must be:

edit 1
    set name "demo"
    set uuid demo
    set srcintf "demo-b-demo"
    set dstintf "demo"
    set srcaddr "demo.demo.demo.110" "host_10.demo.demo.129"
next
edit 2
    set name "demo rule"
    set uuid demo-c532-51eb-end-demo
    set srcintf "end-demo-demo"
    set dstintf "demo"
    set srcaddr "demo.40.demo.129" "demo.40.demo.110"
next

I tried something in Python result = re.findall('config firewall policy\s([\s\S]*?)end', str(abovetext))

0

1 Answer 1

1

You can leverage the fact these delimiters are on separate lines.

That means, you can use

re.findall(r'(?m)^config firewall policy\r?\n\s*([\s\S]*?)\s*\nend$', text)

See this regex demo.

Details:

  • (?m) - the re.M inline flag variant
  • ^ - start of a line
  • config firewall policy - a string of words
  • \r?\n - CRLF or an LF line break sequence
  • \s* - zero or more whitespaces
  • ([\s\S]*?) - Group 1: any zero or more chars, as few as possible
  • \s* - zero or more whitespaces
  • \n - a newline
  • end$ - a line with only end text on it.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.