Using https://regex101.com/ I built a regular expression to return the first occurrence of an IP address in a string.
RegExp:
(?:\d{1,3}\.)+(?:\d{1,3})
RegExp including delimiters:
/(?:\d{1,3}\.)+(?:\d{1,3})/
With the following test string:
eu-west 140.243.64.99
It returns a full match of:
140.243.64.99
No matter what I try with anchors etc, the following bash script will not work with the regular expression generated.
temp="eu-west 140.243.64.99 "
regexp="(?:\d{1,3}\.)+(?:\d{1,3})"
if [[ $temp =~ $regexp ]]; then
echo "found a match"
else
echo "No IP address returned"
fi
=~
operator is discussed here in the manual where it's written bash uses "extended regular expressions". Extended regexes are described in theregex(7)
man page and briefly summarized here. – glenn jackman Feb 02 '18 at 15:57