To identify files with the hyphen symbol -
in file names such as test-19.1.txt
, the find
command combined with a regular expression does not appear to match.
The command find . -maxdepth 1 -regextype posix-egrep -regex '.*/[a-z0-9\-\.]+\.txt' -exec echo {} \;
is run in a bash shell and no such file is discovered. If the hyphen is removed from the filename, the regular expression matches.
The same regular expression when tested with regexr.com is successful.
Best Answer
To include a hyphen in a character class it must be at the first or last position
From find manual "the type of regular expression used by find and locate is almost identical to that used in GNU Emacs" and from Emacs manual:
So your regex should be
'.*/[a-z0-9.-]+\.txt'
In POSIX BRE & ERE the same rule applies
In fact most regex variants has the same rule for matching hyphen