1

I'm trying to play with regular expressions in bash but i can't understand why this follow scenario doesn't work :

Regexp:

REGEXP="^(test\/)(([a-zA-Z]+)\-)+(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$"

String:

STRING="test/test-ods-1.10.1"

Test:

if [[ "$STRING" =~ $REGEXP ]]
then
       echo "match!"
else
       echo "don't match"
fi

Normally in this scenario I should receive a "match" but it's always returning a "doesn't match".

1
  • Just a small hint [[ "foo" =~ foo ]]; echo $? shows 0 in case of a match, 1 if not, and 2 if there is a syntax error in the regex. Commented Sep 2, 2016 at 9:36

1 Answer 1

3

\d is not defined in bash regexps. Replace them with [0-9] and it'll work :

$ [[ "test/test-1.10.1" =~ ^(test\/)(([a-zA-Z]+)\-)+(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$ ]]; echo $?
1

$ [[ "test/test-1.10.1" =~ ^(test\/)(([a-zA-Z]+)\-)+(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; echo $?
0

No shorthand classes are defined in POSIX, and GNU extensions only bring a few of them, \w, \W, \s and \S according to regular-expressions.info.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.