Bash and regexp for pattern match with varying numbers of digits

Trying to do this pattern match using "ls" and I just can't seem to get it right.

Create a file:

touch synaptic-0.91.3

Using the following commands sequence

packageName="synaptic-0.91.3"
find . -regextype posix-extended -regex '.*[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}' -print | cut -c3- | grep '^'"${packageName}"

we get the correct result, namely "synaptic-0.91.3".

However, looking at various examples and references, I've tried many different patterns, none of which have worked for application with our humble and straightforward "ls".

The pattern which I expected to work, but doesn't was this:

ls -d "${packageName}"-[[:digit:]]+.[[:digit:]]+.[[:digit:]]+

After a lot of looking around, and wasted time, I located the following method that gives the correct result:

shopt -s extglob
ls -d "${packageName}"-+([0-9]).+([0-9]).+([0-9]) 

That shopt command is the one that allows us to "link in" extra pattern matching capabilities to match some of those of other languages.

Each "+([0-9])" sequence matches one or more digits, which is what I was trying to get. :slight_smile:

Out of curiosity, I tried the following, and it also gives the correct result:

oomd="+([0-9])"
ls -d "${packageName}"-${oomd}.${oomd}.${oomd}

BUT, the following form will fail because it prevents the shell's action to expand the regular expression:

ls -d "${packageName}-${oomd}.${oomd}.${oomd}"
3 Likes