1

I've been trying to match a single IPv4 address out of an xml string and bind it to a variable for later use. I've tested the regex itself in some online testers and it appears to function with some sample outputs. I've also tested the BASH_REMATCH similar to the one below with a much simpler regex and pattern and it works. So is there a problem with my regex itself not compatible with bash?

Sample Script...

#!/bin/bash
rawxml="<ip-netmask>1.2.3.4/32</ip-netmask>"
ipv4regex="(((?:|\.)(2(?:5[0-5]|[0-4]\d)|1?\d?\d)){4})"

# Finding IP from $rawxml and binding it to its own variable
if [[ $rawxml =~ $ipv4regex ]]; then
        ipfound=${BASH_REMATCH[1]}
fi

echo "IP found is "$ipfound""

Expected output here would be....

"IP Found is 1.2.3.4"

0

1 Answer 1

1

With and :

xmllint --xpath '//ip-netmask/text()' file.xml

A real XML parser is a better tool than regex to parse XML

Sign up to request clarification or add additional context in comments.

2 Comments

Noted. Will give this a go.
Confirmed this was 1000x easier than trying to use regex. Updating OP with new method

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.