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
Best Answer
\d
is a nonstandard way for saying "any digit". I think it comes from Perl, and a lot of other languages and utilities support Perl-compatible REs (PCRE), too. (and e.g. GNU grep 2.27 in Debian stretch supports the similar\w
for word characters even in normal mode.)Bash doesn't support
\d
, though, so you need to explicitly use[0-9]
or[[:digit:]]
. Same for the non-capturing group(?:..)
, use just(..)
instead.This should print
match
: