The string I'm trying to match is an ip address and I've seens quite some examples on the web. However it appears that I'm missing something which may or may not have to do with the differences between regexp conventions. (pcre, ere, ??)
To break it down to the point I'm missing here's the partial ip address that I'm trying to get matched:
ip="255.255."
This is a regular expression that tries to match above ip:
^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){2}
It's bash so I've got this to compare:
[[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){2} ]] && echo "ok"
Alas, it doesn't match.
As far as I know, I'm allowed to escape the . because I want it to match a point character, not just any character. (what . stands for in regexp to the best of my knowledge.)
This is what happens when I remove the escape before the point to illustrate above statement:
# ip="255.255."
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?.){2} ]] && echo "ok"
ok
# ip="255X255Y"
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9] [0-9]?.){2} ]] && echo "ok"
ok
That's not correct to match (partial) ip in this case.
Why does it not match this:
# ip="255.255."
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){2} ]] && echo "ok" || echo "nope"
nope
There are two groups and each of them is "255." and that part matches just fine like this:
# ip="255."
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.) ]] && echo "ok" || echo "nope"
ok
EDIT: some extra info that possibly solves my issue:
just noticed the following:
# ip="172.15.11.10"
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){2} ]] && echo "ok" || echo "nope"
ok
# ip="172.15."
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){2} ]] && echo "ok" || echo "nope"
ok
And when the following grouping is made:
# [[ ${ip} =~ ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){2} ]] && echo "ok" || echo "nope"
nope
# [[ ${ip} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2} ]] && echo "ok" || echo "nope"
ok
So grouping the "number" part in front of the seperator "point" solves the issue?
# ip="255.255."
# [[ ${ip} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){2} ]] && echo "ok" || echo "nope"
ok
I'm inclined to think this solves it however I do not yet understand why.
EDIT: the full regexp for the ip address is this here:
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
This is the same mentioned by Jeff in below answer.