How to check if a string contains version in numberic/decimal format in shell script
for eg we have 1.2.3.5 or 2.3.5
What if we do not have a constraint on the number of characters we have in here. It could x.x.x.x or x.x as well.
How to check if a string contains version in numberic/decimal format in shell script
for eg we have 1.2.3.5 or 2.3.5
What if we do not have a constraint on the number of characters we have in here. It could x.x.x.x or x.x as well.
Flip the logic: check if it contains an "invalid" character:
Downside to this answer:
If you're using
bash
, you can use the=~
regex match binary operator, for example:For your particular test data, the following regex will do the trick:
(a number followed by any quantity of
.<number>
extensions) although, if you want to handle edge cases like1.2-rc7
or4.5-special
, you'll need something a little more complex.Using bash regular expressions:
This accepts
digits.digits.digits
ordigits.digits.digits.digits
Change
{2,3}
to shrink or enlarge the acceptable number of.digits
(or{2,}
for "at least 2")^
means beginning of string[0-9]+
means at least one digit\.
is a dot(...){2,3}
accepts 2 or 3 of what's inside the()
$
means end of stringAnd if you're truly restricted to the Bourne shell, then use expr:
I'm sure there are solutions involving awk or sed, but this will do.