I want to exclude any files ending with '.ses' or files with no extension using the following regex pattern. It works fine in command line but not in a shell (bash/ksh).
Regex pattern: "\.(?!ses\$)([^.]+\$)"
File name examples:
"/test/path/test file with spaces.__1" (expected true)
"/test/path/test file with spaces.ses" (expected false)
"/test/path/test file with spaces" (expected false)
"/test/path/test file with spaces.txt" (expected true)
FILE_NAME="/test/path/test file with spaces.__1"
PATTERN_STR="\.(?!ses\$)([^.]+\$)"
if [[ "${FILE_NAME}" =~ ${PATTERN_STR} ]]; then
Match_Result="true"
else
Match_Result="false"
fi
echo $Match_Result
it returns "true" but "false" in the shell. Anyone knows why?
(?!...)is PCRE. Bash implements "extended regular expressions" like grep -E. What was you "command line" command you were using?[[ "${FILE_NAME##*/}" =~ \.ses$|^[^.]+$ ]] && exclude=yes. You need to strip any leading path. But I would generally use a case statement, like Allan's answer. It's a lot clearer.