Regex validation doesn't work in bash -
Regex validation doesn't work in bash -
i'd utilize next regex in order validate project version numbers:
(?!\.)(\d+(\.\d+)+)([-.][a-z]+)?(?![\d.])$
demo
valid inputs:
1.0.0-snapshot
1.0.0.rc
1.0.0
i'm trying utilize script follows:
#!/bin/bash r=true; p="(?!\.)(\d+(\.\d+)+)([-.][a-z]+)?(?![\d.])$" while [ $r == true ]; echo "get_v: " read v; if [[ $v =~ $p ]]; echo "ok"; r=false else echo "nok" fi done
but returns me nok using valid inputs.
what doing wrong?
bash doesn't supports pcre
- perl regular expressions. supports extended regular expr - ere.
you can check string grep -p
like:
while read -r ver res=$(grep -op '(?!\.)(\d+(\.\d+)+)([-.][a-z]+)?(?![\d.])$' <<<"$ver") echo "ver:$ver status:$? result:=$res=" done <<eof | column -t 1 1-release 1.2 1.2-dev1 1.0.0-release 1.0.0.3 q x-release 1.x z.2 1.x-dev1 1.x.0-dev3 .1.0-dev3 v1 v1.3-snapshot eof
however, recheck regex, because above prints:
ver:1 status:1 result:== ver:1-release status:1 result:== ver:1.2 status:0 result:=1.2= ver:1.2-dev1 status:1 result:== ver:1.0.0-release status:1 result:== ver:1.0.0.3 status:0 result:=1.0.0.3= ver:q status:1 result:== ver:x-release status:1 result:== ver:1.x status:1 result:== ver:z.2 status:1 result:== ver:1.x-dev1 status:1 result:== ver:1.x.0-dev3 status:1 result:== ver:.1.0-dev3 status:1 result:== ver:v1 status:1 result:== ver:v1.3-snapshot status:0 result:=1.3-snapshot=
i use
r='((?<=\a)|(?<=\s))v?\d+(\.\d+)*(-\w+)?(?=(\s|\z))'
e.g.:
r='((?<=\a)|(?<=\s))v?\d+(\.\d+)*(-\w+)?(?=(\s|\z))' while ifs= read -r ver res=$(grep -op "$r" <<<"$ver") printf "ver:%-15.15s status:%s result:=%s=\n" "$ver" $? "$res" done <<eof 1 1-release 1.2 1.2-dev1 1.0.0-release 1.0.0.3 q x-release 1.x z.2 1.x-dev1 1.x.0-dev3 .1.0-dev3 v1 v1.3-snapshot eof
prints:
ver:1 status:0 result:=1= ver: 1-release status:0 result:=1-release= ver: 1.2 status:0 result:=1.2= ver:1.2-dev1 status:0 result:=1.2-dev1= ver:1.0.0-release status:0 result:=1.0.0-release= ver:1.0.0.3 status:0 result:=1.0.0.3= ver:q status:1 result:== ver:x-release status:1 result:== ver:1.x status:1 result:== ver:z.2 status:1 result:== ver:1.x-dev1 status:1 result:== ver:1.x.0-dev3 status:1 result:== ver:.1.0-dev3 status:1 result:== ver:v1 status:0 result:=v1= ver:v1.3-snapshot status:0 result:=v1.3-snapshot=
if don't want allow v1.1
- e.g. v
@ origin remove v?
regex.
if want more restrictive regex, utilize the
r='((?<=\a)|(?<=\s))\d+(\.\d+){1,2}(-[a-z]+)?(?=(\s|\z))'
will allows 2 or 3 numbers , uppercase after -
.
and finally, if want pure bash - utilize ere next:
r='^[0-9]+(\.[0-9]+)+(-[a-z]+)?$' while read -r ver [[ $ver =~ $r ]] && echo "$ver: ok" || echo "$ver: no" done <<eof | column -t 1 1-release 1.2 1.2-dev 1.2-dev2 1.0.0-release 1.0.0 1.0.0.3 q x-release 1.x z.2 1.x-dev 1.x.0-dev .1.0-dev v1 v1.3-snapshot eof
prints
1: no 1-release: no 1.2: ok 1.2-dev: ok 1.2-dev2: no 1.0.0-release: ok 1.0.0: ok 1.0.0.3: ok q: no x-release: no 1.x: no z.2: no 1.x-dev: no 1.x.0-dev: no .1.0-dev: no v1: no v1.3-snapshot: no
regex bash
Comments
Post a Comment