If you don't mind using multiple steps this could be a solution.
out1 = """declare -a IP_LIST=(
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/28'
)"""
data = re.search(r"IP_LIST=\(((\s*'([x./0-9]+)')+)", out1) # , re.MULTILINE)
print(data.group(1))
You might want to remove the "x" in [x./0-9]
for a real ip address.
This will give you
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/24'
'x.x.x.x/28'
Based on that result we can continue with simple string operations
result = [ip.strip("' ") for ip in data.group(1).strip().split('\n')]
print(result)
The result now is ['x.x.x.x/24', 'x.x.x.x/24', 'x.x.x.x/24', 'x.x.x.x/24', 'x.x.x.x/24', 'x.x.x.x/28']
.
If you want to remove the subnet too then change the line where you create the result to
result = [ip.split('/')[0].strip("' ") for ip in data.group(1).strip().split('\n')]
To explain the regular expression as requested in the comment:
We have IP_LIST=\(((\s*'([x./0-9]+)')+)
. IP_LIST=\(
matches "IP_LIST=(". This leaves us with ((\s*'([x./0-9]+)')+)
. The outer parentheses are there to define the group that will contain the text we want to get later. Take those away and we have (\s*'([x./0-9]+)')+
.
Let's first focus on \s*'([x./0-9]+)'
. \s*
matches a bunch of potential whitespaces (space, tab, newline, ...). Then we have a literal '
. The following group defines a set of characters: [x./0-9]+
(where 0-9 defines the numbers/characters from 0-9). As you can see all those characters are used in the ip addresses from your example (as I said, the "x" is due to the example data and might be dropped in the final code). The characters in this set can and will be repeated multiple times so we add the plus sign: [x./0-9]+
. The final character is again a literal '
.
Now comes the fun part. We have just explained \s*'([x./0-9]+)'
which defines some whitespaces followed by an ip address. This might be repeated several times over multiple lines. So we wrap this in parentheses and add a +
to allow repetitions of this part. Now we're back to ((\s*'([x./0-9]+)')+)
.