Correct regex pattern from online tool not working in bash

Viewed 41

I have created the regex pattern shown below using the regexr.com online tool. The pattern captures what I need; but as soon as I move it to bash, I am faced with an empty output.

The Regex: (server[0-9])([\S\s]*?)(IPv4Address": "[0-9]+.+(?=\/))

Part of the text file. What I need are the last two paragraphs starting from the line containing "server" and ending with the line that has the IPv4:

       "5f004e4993402ee707a164f9ba15a9cb81ffa72e7f91291e75a40e431ab2e4dd": {
            "Name": "th_bootstrapper.z05ssmnsws1nus1sltg1osgns.elrwigmzg08uq5v7u1w06ymy8",
            "EndpointID": "1fc403412422c6424e636ff53667bd769afe503364f5d2cb47db6e1f2bf4ea7d",
            "MacAddress": "02:42:0a:01:00:13",
            "IPv4Address": "10.1.0.19/24",
            "IPv6Address": ""
        }, "6277e76294ef7021db632848f151102bd2c76c208f64ccbeac1dd8a9f202caf1": {
            "Name": "th_server4-6113a162-7314-4fb9-a5b1-4205ce7a485d.1.rmy1jsvlfufsz2bgwjjnfccii",
            "EndpointID": "ce805f2ef8c0e42e768c4c4b93bd56a9eab93be139e6fb7ee3cc96936116ab00",
            "MacAddress": "02:42:0a:01:00:17",
            "IPv4Address": "10.1.0.23/24",
            "IPv6Address": ""
        },
        "78a4016c0ee2a93f0264221eab4995477ff7c42e1f94a8773799eb3d825be06d": {
            "Name": "th_server0-6113a162-7314-4fb9-a5b1-4205ce7a485d.1.y5lztwzjzwiv4167nyg7d76bl",
            "EndpointID": "def8c4ed39a32a2ac53c068f46964316a33d586a38b24b51ef0cf02fd4c1479c",
            "MacAddress": "02:42:0a:01:00:1a",
            "IPv4Address": "10.1.0.26/24",
            "IPv6Address": ""
        },

Here's a link to the same regex and text snippet: https://regex101.com/r/y0qMEi/3

What had helped me in the past was either using the 'egrep' or the grep flags: 'grep -E' and 'grep -P'. None of these solve the issue this time.

I am new to regex and bash world and I would appreciate it if anyone could help me with this problem.

1 Answers

Typically regex lookahead and lookbehind are not supported by all regex flavours. You could rewrite your regex to not use these features:

(server[0-9])([\S\s]*?)(IPv4Address": "[0-9]+[^\/]+)

some snippets explained...
using [^\/]+ ="any character expect / more than 1 times"
instead of .+(?=\/) ="any character more than 1 times before the next / character"

Related