-1

I tried this code but it doesn't work; I cannot catch anything. I need to get a multiline match and have been working 3 days on it now. Thanks for your help!!

My regex:

print(re.findall(r'^ltm\s+pool\s+/Common/[0-9-A-Z_.-]+\s+\{([\s\S]*?)^\}',file.read(), re.MULTILINE))
print(re.findall(r'^ltm\s+pool\s+/Common/[0-9-A-Z_.-]+\s+\{(.*?)^\}',file.read(), re.DOTALL))

My code:

#!/usr/bin/env python3
import re, os, sys

### We create a new file
f = open("bigip.txt", "w")

### Default stdout value copied to a variable
orig_stdout = sys.stdout

### Stdout transfered to a file in write mode
sys.stdout = open("bigip.txt", "w")


file = open("bigiptemp", "r")
#for line in file:
    #if re.findall(r'^ltm\spool\s\/Common\/([A-Z-a-z]+)', line):
        #print(line)

print(re.findall(r'^ltm\s+pool\s+/Common/[0-9-A-Z_.-]+\s+\{([\s\S]*?)^\}',file.read(), re.MULTILINE))

### Default stdout reset
sys.stdout = orig_stdout

The file below is an extract:

ltm pool /Common/GEOG.GD {
    members {
        /Common/
            address 
        }
        /Common/
            address 
    }
    monitor
}

ltm pool /Common/HAP_NAODE_DEV {
    members {
        /Common
            address
        }
        /Common
            address
        }
    }
    monitor
}

The expected behavior is the following but I cannot share the content of bigiptemp ... But my previous answer was tagged as duplicate ... Regular expression works on regex101.com, but not on prod

Expected result

  • Is the "extract" text just a part of the `bigiptemp` file? With `file = open("bigiptemp", "r")`, you open the `bigiptemp` file, can you share it somehow? You may just paste its contents into pastebin.com and share the URL. Also, note that the second `re.findall` will only work if you add `re.M` to the flags: `re.findall(r'^ltm\s+pool\s+/Common/[0-9-A-Z_.-]+\s+\{(.*?)^\}',file.read(), re.DOTALL|re.M)` (see [demo](https://regex101.com/r/C3ezUu/1)). – Wiktor Stribiżew Nov 02 '20 at 10:26
  • 1
    What is your actual expected output, based on the extract you showed above? – Tim Biegeleisen Nov 02 '20 at 10:26
  • 4
    give an example of an excepted result – Alireza Nov 02 '20 at 10:26
  • Not sure what you expect, but this looks okay to me: https://regex101.com/r/zzfrcL/1 – swenzel Nov 02 '20 at 10:38
  • I answered your questions in the bulk. – Gueug78400 Nov 02 '20 at 11:29

2 Answers2

-1

try this

#!/usr/bin/env python3
import re, os, sys

### We create a new file
f = open("bigip.txt", "w")

### Default stdout value copied to a variable
orig_stdout = sys.stdout

### Stdout transfered to a file in write mode
sys.stdout = open("bigip.txt", "w")


file = open("bigiptemp", "r")
#for line in file:
    #if re.findall(r'^ltm\spool\s\/Common\/([A-Z-a-z]+)', line):
        #print(line)

print(*re.findall(r'^ltm\s+pool\s+/Common/[0-9-A-Z_.-]+\s+\{([\s\S]*?)^\}',file.read(), re.MULTILINE))

### Default stdout reset
sys.stdout = orig_stdout

it seems work and match excepted results

Alireza
  • 2,103
  • 1
  • 6
  • 19
  • I tried as you said: `# ./test.py Traceback (most recent call last): File "./test.py", line 21, in print(find[0], find[1]) IndexError: list index out of range` – Gueug78400 Nov 02 '20 at 11:53
  • if it works, consider that click on ✓ to accept this answer. – Alireza Nov 02 '20 at 12:17
  • The OP code also matches expected results. What have you fixed? – Wiktor Stribiżew Nov 02 '20 at 16:51
  • I'm new in SO and don't know whats meaning OP but I guess your intention was "original post", anyway the previous code just print two first index of the list but after edited my answer this new code prints all of the element in the list – Alireza Nov 02 '20 at 17:13
  • Unfortunately, I could not make it work ... There must be a way to match all data inside curly braces preceded by a topic like "ltm pool" for example. – Gueug78400 Nov 02 '20 at 18:06
  • can you share exactly the code that you use for test? use pastebin.com to share that – Alireza Nov 02 '20 at 18:10
-2

My solution is this (three days to approximately understand how regex works):

regex = r'(^ltm\s+pool\s+/Common/[0-9-A-Z_.-]+\s+\{[\s\S]*?^\}\n?)'

print(*(re.findall(regex, file.read(), re.MULTILINE)))

ltm pool /Common/GEOG.GD {
    members {
        /Common/
            address 
        }
        /Common/
            address 
    }
    monitor
}

ltm pool /Common/HAP_NAODE_DEV {
    members {
        /Common
            address
        }
        /Common
            address
        }
    }
    monitor
}

Never use like me 'for line in file' if you want to catch several lines in a grasp (one-shot) ... logic!!!

A very good regex-topic book for the insomniacs ... lol:

=> https://www.princeton.edu/~mlovett/reference/Regular-Expressions.pdf

Thanks to you all!! Thanks to @Wiktor Stribiżew

@gueug